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类型存储而不失其精确性.该类型对于必须避免舍入错误的应用程序( ...
随机推荐
- Unknown command '\b'. 关于Mysql导入外部数据库脚本报错的解决
来自网络转载 还是字符集的问题 使用source导入外部sql文件: mysql> source F:\php\bookorama.sql;--------------source F:---- ...
- 12.tomcat7切换tomcat8导致cookie异常
一.现象 换成Tomcat8后出现cookie报错 二.分析 经异常去查看源码发现,Tomcat8对cookie校验规则改变,更为严格的校验了cookieHeader不允许有, 日志中的[XXXXX, ...
- google 人机身份验证
google 人机身份验证 Are you a robot? Introducing "No CAPTCHA reCAPTCHA" https://googleonlinesecu ...
- auto switch HTTP protocol Chrome Extension
auto switch HTTP protocol Chrome Extension HTTPS auto switch to HTTP VPN https://chrome.google.com/w ...
- D3 tree map
D3 tree map D3 矩形树图 https://www.zhihu.com/question/55529379 https://zhuanlan.zhihu.com/p/57873460 ht ...
- taro best practice
taro best practice 最佳实践 https://taro-docs.jd.com/taro/docs/best-practice.html#关于-jsx-支持程度补充说明 https: ...
- cURL all in one
cURL all in one convert http request to curl online https://curlbuilder.com/ https://cdn.xgqfrms.xyz ...
- 为什么说NGK引领全球数字资产财富革命
进入2020年,区块链万业迸发出巨大的能量,事实上区块链和数字经济正是未来全球财富的新方向.区块链和数字货币的增值空间巨大,是数字时代新的经济增长点.目前,全球衍生品市场价值532万亿美元,全球债务市 ...
- BGV上线17小时最高888.88美金,投资最高回报率近+1778倍, 带动NGK内存暴涨
至12月3日BGV币上线A网交易所DeFi板块以来,BGV价值飙升长.,据非小号的数据显示,BGV币价是718美元(东八区时间2020年12月4日早上九点四十),相较昨日涨幅达70.14%,以718美 ...
- Centos7 升级 sqlite3
下载地址:https://www.sqlite.org/download.html [root@djangoServer ~]# wget https://www.sqlite.org/2019/sq ...