MySQL中where和on,where和having 的区别
where和on的区别
用到连接查询时on会常用到,我们以左连接为例,来了解on的作用。
on是在生成临时表使用的条件,不管on子句的条件是否为真,其都会返回左表的数据,如果条件为真则右表对应的数据也将会显示,如果为假则只返回左表的数据对应的有表的数据为null。
实例演示:
创建数据表
create table fruits(
f_id int(11) primary key auto_increment,
f_name varchar(50) not null,
f_price float not null ); create table customers(
c_id int(11) primary key auto_increment,
fruits_id int(11) not null,
foreign key(fruits_id) references fruits(f_id)
);
#插入数据
insert into fruits(f_id,f_name,f_price) values
(1,'apple',10),
(2,'banalan',5),
(3,'anrange',6);
insert into customers(c_id,fruits_id)values
(1,2),
(2,2),
(3,1);
on单条件查询
select c_id ,f_price
from
customers left outer join fruits
on
customers.fruits_id = fruits.f_id;

on多条件查询
select c_id ,f_price
from
customers left outer join fruits
on
customers.fruits_id = fruits.f_id and fruits.f_id = 1;

从两个结果中可以看出查询结果的总数并没有发生变化,实际上on的子句的作用是筛选连接表(fruits)要显示的内容,并不影响查询结果的条数。
在on后加上where语句
select c_id ,f_price
from
customers left outer join fruits
on
customers.fruits_id = fruits.f_id where fruits.f_id = 1;

查询结果变为一条ON后的WHERE子句的实际作用是 对多表连接的结果进行筛选,满足条件的记录才能被留下,所以他会影响 最终的查询记录数。
where和having的区别
原文链接:https://blog.csdn.net/yexudengzhidao/article/details/54924471
1. where和having都可以使用的场景
select goods_price,goods_name from sw_goods where goods_price > 100
1
select goods_price,goods_name from sw_goods having goods_price > 100
1
解释:上面的having可以用的前提是我已经筛选出了goods_price字段,在这种情况下和where的效果是等效的,但是如果我没有select goods_price 就会报错!!因为having是从前筛选的字段再筛选,而where是从数据表中的字段直接进行的筛选的。
2. 只可以用where,不可以用having的情况
select goods_name,goods_number from sw_goods where goods_price > 100
1
select goods_name,goods_number from sw_goods having goods_price > 100 //报错!!!因为前面并没有筛选出goods_price 字段
1
3. 只可以用having,不可以用where情况
查询每种goods_category_id商品的价格平均值,获取平均价格大于1000元的商品信息
select goods_category_id , avg(goods_price) as ag from sw_goods group by goods_category having ag > 1000
1
select goods_category_id , avg(goods_price) as ag from sw_goods where ag>1000 group by goods_category //报错!!因为from sw_goods 这张数据表里面没有ag这个字段
1
注意:where 后面要跟的是数据表里的字段,如果我把ag换成avg(goods_price)也是错误的!因为表里没有该字段。而having只是根据前面查询出来的是什么就可以后面接什么。
MySQL中where和on,where和having 的区别的更多相关文章
- MySQL 中 key, primary key ,unique key,index的区别
一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_n ...
- 【转】Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结
Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...
- mysql中时间类型datetime,timestamp与int的区别
在mysql中存储时间,我们可以用datetime 格式,timestamp格式,也可以用int格式.那么我们设计的时候该如何考虑呢? 首先,我觉得应该明白这几个格式究竟是如何的,然后看看他们的区别, ...
- Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结
Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...
- MYSQL中的普通索引,主健,唯一,全文索引区别
MYSQL索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的记 ...
- MySql中的count、NULL和空串的区别
**1.count (1).count (*) 与 count (列名) 的区别** 表 count(1) count(*) count (列名) 作用 统计表中的所有的记录数 会统计表中的所有的记录 ...
- 浅谈SQL Server、MySQL中char,varchar,nchar,nvarchar区别
最近一次的面试中,被面试官问到varchar和nvarchar的区别,脑海里记得是定长和可变长度的区别,但却没能说出来.后来,在网上找了下网友总结的区别.在这里做个备忘录: 一,SQL Server中 ...
- mysql中char,varchar与text类型的区别和选用
关于char,varchar与text平时没有太在意,一般来说,可能现在大家都是用varchar.但是当要存储的内容比较大时,究竟是选择varchar还是text呢?不知道...... 于是去查阅了一 ...
- mysql中几个日期时间类型之间的区别和使用
MySQL中有如下几个时间类型:date.time.datetime.timestamp.year MySQL数据类型 含义 date 只存 ...
- MySQL中的float和decimal类型有什么区别
decimal 类型可以精确地表示非常大或非常精确的小数.大至 1028(正或负)以及有效位数多达 28 位的数字可以作为 decimal类型存储而不失其精确性.该类型对于必须避免舍入错误的应用程序( ...
随机推荐
- Spring(四) SpringDI(1)
Spring 自动装配之依赖注入 依赖注入发生的时间 当 Spring IOC 容器完成了 Bean 定义资源的定位.载入和解析注册以后,IOC 容器中已经管理类 Bean 定义的相关数据,但是此时 ...
- 指纹采集器Live 20R
最近有个项目需要使用指纹采集器Live 20R,买来这个小玩意后不知道怎么用,看了一些教程和自己摸索了一下,才初步掌握了用的方法. 环境: 硬件:联想 小新 操作系统:Win 10 IDE:VS201 ...
- Storybook 最新教程
Storybook 最新教程 Storybook is the most popular UI component development tool for React, Vue, and Angul ...
- 如何在 Apple Watch S6上离线播放音乐
如何在 Apple Watch S6上离线播放音乐 Apple Watch 离线播放音乐 营销策略,捆绑销售 Apple Watch + AirPods + Apple Music Apple Wat ...
- Node.js _dirname & path All In One
Node.js _dirname & path All In One file path 相对路径 绝对路径 _dirname https://nodejs.org/docs/latest/a ...
- 2016 JS 笔试题汇总:
1 1 1 CS&S(中软国际): 1 JavaScript 循环表达式: 2 JavaScript表达式boolean返回值: 3 网页中的事件/HTML 事件属性/JavaScript ...
- Headless Chrome Node API
puppeteer Headless Chrome Node API https://github.com/GoogleChrome/puppeteer https://pptr.dev/ PWA h ...
- 微前端 & 微前端实践 & 微前端教程
微前端 & 微前端实践 & 微前端教程 微前端 micro frontends https://micro-frontends.org/ https://github.com/neul ...
- Flutter Navigator2.0
Example 1 import 'package:dart_printf/dart_printf.dart'; import 'package:flutter/material.dart'; cla ...
- C++算法代码——质因数分解[NOIP2012普及组]
题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1102 题目描述 已知正整数 n 是两个不同的质数的乘积,试求出较大的那个质数. 输入 ...