SQL中where与having的区别
发布日期:2021-06-29 05:44:48
浏览次数:2
分类:技术文章
本文共 1197 字,大约阅读时间需要 3 分钟。
SQL中where与having的区别
目录
# WHERE是在聚合之前进行数据行的过滤而HAVING实在聚合之后进行新数据的过滤;
即在SQL语法中WHERE语法的执行早于GROUP BY,而GOUP BY 早于HAVING的执行;
# WHERE
filters rows before grouping (i.e. GROUP BY
) while HAVING
filters rows after grouping.
创建表
包含字段:名称、年龄、种族、使用的装备
-- Create table called adventurersCREATE TABLE adventurers ( -- string variable name varchar(255), -- integer variable age int, -- string variable race varchar(255), -- string variable weapon varchar(255))
插入数据
在表中插入四条数据
-- Insert into the table adventurersINSERT INTO adventurers (name, age, race, weapon)VALUES ('Fjoak Doom-Wife', 28, 'Human', 'Axe'), ('Alooneric Cortte', 29, 'Human', 'Bow'), ('Piperel Ramsay', 35, 'Elf', 'Axe'), ('Casimir Yardley', 14, 'Elf', 'Bow')
进行聚合操作
使用where筛选年龄大约15岁的角色
-- Retrieve the race and average age from the tableSELECT race, AVG(age) FROM adventurers-- Grouped by raceGROUP BY race-- That are older than 15WHERE age > 15
使用having,在聚合后的结果中选取年龄大约20的结果;
-- Retrieve the race and average age from the tableSELECT race, age, AVG(age) FROM adventurers-- Grouped by raceGROUP BY race age-- That are older than 20HAVING age > 20
参考:Group Rows With Conditions
参考:SQL
参考:SQL中where与having的区别
转载地址:https://blog.csdn.net/zhongkeyuanchongqing/article/details/117473829 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月04日 21时49分39秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
DataWhale & Golang(十二、单元测试)
2019-04-29
DataWhale & Pandas(四、分组)
2019-04-29
DataWhale & Pandas (缺失数据)
2019-04-29
DataWhale & Pandas (文本数据)
2019-04-29
DataWhale & Pandas(分类数据)
2019-04-29
idea时jsp中使用out.write();时报错的解决办法
2019-04-29
EL 表达式的 11 个隐含对象
2019-04-29
EL表达式(pageContext)
2019-04-29
EL 表达式其他隐含对象的使用
2019-04-29
DataWhale & Pandas(时序数据)
2019-04-29
DW&LeetCode_day1(2、4、5)
2019-04-29
题目 1471:【蓝桥杯】矩形面积交
2019-04-29
DW&LeetCode_day12(146、148、155)
2019-04-29
B站_黑马SSM_p16_空指针
2019-04-29
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; 不允许有匹配 “[xX][mM][lL]“
2019-04-29
DW&LeetCode_day13(160、169、206)
2019-04-29