MySQL中having關鍵字詳解以及與where的區(qū)別
1、having關鍵字概覽
1.1、作用
- 對查詢的數(shù)據(jù)進行篩選
1.2、having關鍵字產(chǎn)生的原因
- 使用where對查詢的數(shù)據(jù)進行篩選時,where子句中無法使用聚合函數(shù),所以引出having關鍵字
1.3、having使用語法
- having單獨使用(不與group by一起使用,在Oracle中會報錯),單獨使用時,大部分場合與where相同
- having與group by一起使用,這是having關鍵字產(chǎn)生的初衷,對分組之后的數(shù)據(jù)再進行篩選
1.4、having與where的區(qū)別
- 一般情況下,where用于過濾數(shù)據(jù)行,而having用于過濾分組(能用where的地方,不要使用having)
- where中不能出現(xiàn)聚合函數(shù),而having可以使用聚合函數(shù)作為條件
- where在數(shù)據(jù)分組前進行過濾,而having在數(shù)據(jù)分組后進行過濾(因此where效率一般比having高);where是數(shù)據(jù)從磁盤讀入內存時篩選,而having是在內存中篩選
- where是對數(shù)據(jù)庫文件過濾(過濾條件是表中的字段),而having是對select中查詢的字段進行過濾
- where子句中不能使用字段別名,而having子句中可以使用字段別名
- 多表關聯(lián)查詢時,where先篩選再聯(lián)接,having先聯(lián)接再篩選
2、having案例
初始化表(以student表為例):
create table if not exists student ( id int null, name varchar(50) null, age int null, sex varchar(2) null, score double null ) comment '學生表'; INSERT INTO student (id, name, age, sex, score) VALUES (1, '張三', 18, '男', 70); INSERT INTO student (id, name, age, sex, score) VALUES (2, '李四', 17, '男', 60); INSERT INTO student (id, name, age, sex, score) VALUES (3, '王五', 19, '男', 80); INSERT INTO student (id, name, age, sex, score) VALUES (4, '趙六', 16, '男', 90); INSERT INTO student (id, name, age, sex, score) VALUES (5, '七七', 16, '女', 95); INSERT INTO student (id, name, age, sex, score) VALUES (6, '九九', 17, '女', 85); INSERT INTO student (id, name, age, sex, score) VALUES (7, '十一', 18, '女', 80); INSERT INTO student (id, name, age, sex, score) VALUES (8, '小明', 19, '男', 90); INSERT INTO student (id, name, age, sex, score) VALUES (9, '小軍', 17, '男', 55); INSERT INTO student (id, name, age, sex, score) VALUES (10, '小雷', 19, '女', 60);
2.1、having單獨使用
案例1:查詢學生表中,成績在80分以上的數(shù)據(jù)
select * from student having score >= 80
等同于:
select * from student where score >= 80
having使用的錯誤:
select
id
,name
,age
from student
having score >= 80 -- 報錯,score篩選條件沒有出現(xiàn)在select中
where使用的錯誤:
select
id
,name
,age
,score as fenshu
from student
where fenshu >= 80 -- 報錯,where子句中不能使用字段別名2.2、having與group by一起使用
案例2:求各個年齡段的平均分和年齡
select age,avg(score) from student group by age
如下:

案例3:求學生平均分大于80分的年齡段及平均分
- 這里只能使用having,對平均分進行篩選,使用where會報錯
select
age
,avg(score)
from student
group by age
having avg(score) > 80
-- 結果為16歲案例4:查詢學生年齡平均分大于80分中,男生的信息(姓名,男生的分數(shù))
select
name
,sex
,age
,score
from student
where sex = '男'
group by name,sex,age,score
having avg(score) > 80
結果:

總結
到此這篇關于MySQL中having關鍵字詳解以及與where區(qū)別的文章就介紹到這了,更多相關MySQL having關鍵字與where區(qū)別內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mysql error 1071: 創(chuàng)建唯一索引時字段長度限制的問題
這篇文章主要介紹了mysql error 1071: 創(chuàng)建唯一索引時字段長度限制的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
MySQL使用show?effective?grants查看權限官方解讀
這篇文章主要為大家介紹了MySQL使用show?effective?grants查看權限,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
sphinxql如何得到結果數(shù)及show meta的詳細說明
想用sphinxql只得到結果數(shù)。跟mysql里的count(*)一樣2013-02-02
解決MySQL8.0安裝第一次登陸修改密碼時出現(xiàn)的問題
這篇文章主要介紹了解決MySQL8.0安裝第一次登陸修改密碼時出現(xiàn)的問題,在文章開頭給大家介紹了mysql 8.0.16 初次登錄修改密碼的方法,需要的朋友可以參考下2019-06-06

