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类型存储而不失其精确性.该类型对于必须避免舍入错误的应用程序( ...
随机推荐
- HihoCoder1445 后缀自动机二·重复旋律5(后缀自动机 子串种数)
题意: 询问串的不同子串个数 思路: 后缀自动机每个节点表示以当前字符结尾的一系列后缀,个数为\(maxlen - minlen\),其中\(minlen = maxlen[father]\). 代码 ...
- 多线程(二)多线程的基本原理+Synchronized
由一个问题引发的思考 线程的合理使用能够提升程序的处理性能,主要有两个方面, 第一个是能够利用多核 cpu 以及超线程技术来实现线程的并行执行: 第二个是线程的异步化执行相比于同步执行来说,异步执行能 ...
- IT-ebooks free download website & IT 电子书籍免费下载网站
free ebooks of programming 1. http://www.it-ebooks.info/ http://www.it-ebooks-api.info/ 2. http://ww ...
- DNS & HTTPS bug
DNS & HTTPS bug SSL protocol version bug https://typescript-4.x-tutorials.xgqfrms.xyz/ errors Th ...
- prefetch & preload & prerender & dns-prefetch & preconnect
prefetch & preload & prerender & dns-prefetch & preconnect performance optimization ...
- js 垃圾回收 & js GC
js 垃圾回收 & js GC js GC / js Garbage Collector https://developer.mozilla.org/en-US/docs/Web/JavaSc ...
- js 如何获取某一个月的第一天是周几
js 如何获取某一个月的第一天是周几 calendar ??? padding dates // day = 1 const firstMonthDate = new Date(year + mont ...
- vue & template & v-else & v-for bug
vue & template & v-else & v-for bug nested table bug https://codepen.io/xgqfrms/pen/wvaG ...
- Captain technology INC:全球新能源汽车格局突变
美国能源信息署EIA的统计数据显示,2020年上半年全美含纯电动和插电混动在内的新能源乘用车总销量仅为11.1万辆,同比缩水25%.虽然特斯拉在第三季度靠着13.93万辆的销量迎来了环比387%.同比 ...
- 【HTB系列】靶机Access的渗透测试详解
出品|MS08067实验室(www.ms08067.com) 本文作者:大方子(Ms08067实验室核心成员) Hack The Box是一个CTF挑战靶机平台,在线渗透测试平台.它能帮助你提升渗透测 ...