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 的区别的更多相关文章

  1. MySQL 中 key, primary key ,unique key,index的区别

    一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_n ...

  2. 【转】Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...

  3. mysql中时间类型datetime,timestamp与int的区别

    在mysql中存储时间,我们可以用datetime 格式,timestamp格式,也可以用int格式.那么我们设计的时候该如何考虑呢? 首先,我觉得应该明白这几个格式究竟是如何的,然后看看他们的区别, ...

  4. Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...

  5. MYSQL中的普通索引,主健,唯一,全文索引区别

    MYSQL索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的记 ...

  6. MySql中的count、NULL和空串的区别

    **1.count (1).count (*) 与 count (列名) 的区别** 表 count(1) count(*) count (列名) 作用 统计表中的所有的记录数 会统计表中的所有的记录 ...

  7. 浅谈SQL Server、MySQL中char,varchar,nchar,nvarchar区别

    最近一次的面试中,被面试官问到varchar和nvarchar的区别,脑海里记得是定长和可变长度的区别,但却没能说出来.后来,在网上找了下网友总结的区别.在这里做个备忘录: 一,SQL Server中 ...

  8. mysql中char,varchar与text类型的区别和选用

    关于char,varchar与text平时没有太在意,一般来说,可能现在大家都是用varchar.但是当要存储的内容比较大时,究竟是选择varchar还是text呢?不知道...... 于是去查阅了一 ...

  9. mysql中几个日期时间类型之间的区别和使用

    MySQL中有如下几个时间类型:date.time.datetime.timestamp.year MySQL数据类型           含义 date                     只存 ...

  10. MySQL中的float和decimal类型有什么区别

    decimal 类型可以精确地表示非常大或非常精确的小数.大至 1028(正或负)以及有效位数多达 28 位的数字可以作为 decimal类型存储而不失其精确性.该类型对于必须避免舍入错误的应用程序( ...

随机推荐

  1. tensorflow报错:Attempting to fetch value instead of handling error Internal: failed to get device attribute 13 for device 0: CUDA_ERROR_UNKNOWN:

    就是在spyder跑上一篇文章的代码然后就报错: Attempting to fetch value instead of handling error Internal: failed to get ...

  2. C# 数据类型(2)

    String char的集合 string name = "John Doe"; 双引号,char是单引号string是不可变的,一旦初始化后就不能变了,每次对已存在的string ...

  3. 美团CodeM资格赛第二题

    锦标赛 时间限制:1秒 空间限制:32768K 组委会正在为美团点评CodeM大赛的决赛设计新赛制.比赛有 n 个人参加(其中 n 为2的幂),每个参赛者根据资格赛和预赛.复赛的成绩,会有不同的积分. ...

  4. vue 自动注册全局组件

    vue 自动注册全局组件 vue 注册全局组件的方式 const plugins = { install(Vue) { const requireComponent = require.context ...

  5. 使用 js 实现十大排序算法: 基数排序

    使用 js 实现十大排序算法: 基数排序 基数排序 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  6. 使用 Canvas 实现一个类似 Google 的可视化的页面错误反馈库

    使用 Canvas 实现一个类似 Google 的可视化的页面错误反馈库 iframe 嵌套 iframe iframe 包含 复制的 HTML 页面 和支持可以拖拽的工具栏 鼠标经过上面,智能识别 ...

  7. React Hooks 内部实现原理

    React Hooks 内部实现原理 源码分析 // 链表 React Hooks 原理剖析 refs https://reactjs.org/docs/hooks-intro.html https: ...

  8. gitpod & free online ide

    gitpod & free online ide Dev Environments as Code for modern DevOps teams. https://www.gitpod.io ...

  9. js currying & js 科里化

    js currying & js 科里化 var test = ( function (a){ console.log(`a2 =`, a);// 1 // console.log(`b2 = ...

  10. window resize & resize observer

    window resize & resize observer https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_e ...