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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:不同缺失值(missing)填充(imputation)方法回归模型(Regressor)效果对比
下一篇:特征重要性、特征集成+FeatureUnion、特征选择变换器+ColumnTransformer、标签特征变换+TransformedTargetRegressor、特征质量、自动学习数据中的特征

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月04日 21时49分39秒