MySQL having关键字详解、与where的区别

打印 上一主题 下一主题

主题 969|帖子 969|积分 2907

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1、having关键字概览

1.1、作用



  • 对查询的数据进行筛选
1.2、having关键字产生的原因



  • 使用where对查询的数据进行筛选时,where子句中无法使用聚合函数,所以引出having关键字
1.3、having使用语法



  • having单独使用(不与group by一起使用,在Oracle中会报错),单独使用时,大部门场所与where相同
  • having与group by一起使用,这是having关键字产生的初志,对分组之后的数据再进行筛选
1.4、having与where的区别


  • 一样寻常情况下,where用于过滤数据行,而having用于过滤分组(能用where的地方,不要使用having)
  • where中不能出现聚合函数,而having可以使用聚合函数作为条件
  • where在数据分组前进行过滤,而having在数据分组后进行过滤(因此where效率一样寻常比having高);where是数据从磁盘读入内存时筛选,而having是在内存中筛选
  • where是对数据库文件过滤(过滤条件是表中的字段),而having是对select中查询的字段进行过滤
  • where子句中不能使用字段别名,而having子句中可以使用字段别名
  • 多表关联查询时,where先筛选再联接,having先联接再筛选
2、having案例

初始化表(以student表为例):
  1. create table if not exists student
  2. (
  3.         id int null,
  4.         name varchar(50) null,
  5.         age int null,
  6.         sex varchar(2) null,
  7.         score double null
  8. )
  9. comment '学生表';
  10. INSERT INTO student (id, name, age, sex, score) VALUES (1, '张三', 18, '男', 70);
  11. INSERT INTO student (id, name, age, sex, score) VALUES (2, '李四', 17, '男', 60);
  12. INSERT INTO student (id, name, age, sex, score) VALUES (3, '王五', 19, '男', 80);
  13. INSERT INTO student (id, name, age, sex, score) VALUES (4, '赵六', 16, '男', 90);
  14. INSERT INTO student (id, name, age, sex, score) VALUES (5, '七七', 16, '女', 95);
  15. INSERT INTO student (id, name, age, sex, score) VALUES (6, '九九', 17, '女', 85);
  16. INSERT INTO student (id, name, age, sex, score) VALUES (7, '十一', 18, '女', 80);
  17. INSERT INTO student (id, name, age, sex, score) VALUES (8, '小明', 19, '男', 90);
  18. INSERT INTO student (id, name, age, sex, score) VALUES (9, '小军', 17, '男', 55);
  19. INSERT INTO student (id, name, age, sex, score) VALUES (10, '小雷', 19, '女', 60);
复制代码
2.1、having单独使用

案例1:查询学生表中,结果在80分以上的数据
  1. select * from student having score >= 80
复制代码
等同于:
  1. select * from student where score >= 80
复制代码
having使用的错误:
  1. select
  2.     id
  3.     ,name
  4.     ,age
  5. from student
  6. having score >= 80 -- 报错,score筛选条件没有出现在select中
复制代码
where使用的错误:
  1. select
  2.        id
  3.      ,name
  4.      ,age
  5.      ,score as fenshu
  6. from student
  7. where fenshu >= 80 -- 报错,where子句中不能使用字段别名
复制代码
2.2、having与group by一起使用

案例2:求各个年岁段的平均分和年岁
  1. select age,avg(score) from student group by age
复制代码
如下:

 案例3:求学平生均分大于80分的年岁段及平均分


  • 这里只能使用having,对平均分进行筛选,使用where会报错
  1. select
  2.        age
  3.      ,avg(score)
  4. from student
  5. group by age
  6. having avg(score) > 80
  7. -- 结果为16岁
复制代码
案例4:查询学生年岁平均分大于80分中,男生的信息(姓名,男生的分数)
  1. select
  2.     name
  3.     ,sex
  4.     ,age
  5.     ,score
  6. from student
  7. where sex = '男'
  8. group by name,sex,age,score
  9. having avg(score) > 80
复制代码
结果:


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

温锦文欧普厨电及净水器总代理

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表