当前位置:网站首页>面试突击62:group by 有哪些注意事项?
面试突击62:group by 有哪些注意事项?
2022-07-05 14:32:00 【王磊】
注意:本文以下内容基于 MySQL 5.7 InnoDB 数据库引擎。
1.group by 后面不能加 where
在 MySQL 中,所有的 SQL 查询语法要遵循以下语法顺序:
select from where group by having order by limit
以上语法顺序是不能前后互换的,否则报错。比如我们不能在 group by 之后添加 where 查询语句,否则会出现如下错误:
语法顺序的执行是和 MySQL 的 select 语句执行顺序相关的,select 执行先后顺序如下:
from 阶段 where 阶段 group 阶段 having 阶段 select 阶段 order by 阶段 limit 阶段
注意:其中 select 比较特殊,在进行查询语句编写时,要写在最前面,其余语法顺序要和执行先后顺序保持一致。
2.having 或 group by 可单独使用
having 和 group by 可以单独使用,如下查询所示: 
3.having 和 group by 可使用别名
当 having 单独使用时,它的作用和 where 类似,但又有细微的不同。比如在 where 中不能使用别名,但 having 和 group by 却可以别名。咱们创建一个测试表来演示一下,建表 SQL 如下:
drop table if exists student_score;
create table student_score(
id int primary key auto_increment comment '主键',
name varchar(250) comment '姓名',
math decimal(4,1) comment '数学成绩',
chinese decimal(4,1) comment '语文成绩'
);
insert into student_score(name,math,chinese) values('张三',50,50),('李四',80,80),('王五',90,90);
表中的数据如下图所示:
当我们使用总成绩别名 total 分别在 where 和 having 中使用时,查询结果如下:
从上述结果可以看出,having 查询可以使用 select 中的别名,而 where 不能使用别名。 除了 having 可以使用别名之外,group by 也可以使用别名,如下图所示: 
为什么where不能用别名?为having却可以?
where 中不能使用别名,这和 MySQL 语句执行顺序有关,MySQL 语句执行顺序如下:
from 阶段 where 阶段 group 阶段 having 阶段 select 阶段 order by 阶段 limit 阶段
也就是说,在执行 where 查询时,select 还没执行,因此在 where 中想要使用还未执行的 select 中的别名是不行的。那从上面的执行顺序可以看到,having 执行也在 select 之前,为什么它就可以使用 select 中的别名呢?
这是因为 MySQL 在 5.7.5 之后做了扩展,允许在 having 中使用别名,官方文档中有相应的说明,如下图所示:
MySQL 官方文档地址:https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
PS:group by 能使用别名的原理和 having 类似。
总结
SQL 语句编写一定要遵循此先后顺序:select、from、where、group by、having、order by、limit。其中 having 或 group by 都可单独使用,并且在 MySQL 5.7.5 之后,group by 和 having 可以使用别名查询,但 where 不能使用别名。
是非审之于己,毁誉听之于人,得失安之于数。
公众号:Java面试真题解析
边栏推荐
- Judge whether the variable is an array
- 网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
- Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
- R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
- Strong connection component
- Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
- 【招聘岗位】基础设施软件开发人员
- 有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
- Show strength. In this way, the mobile phone will not be difficult to move forward
- 动态规划
猜你喜欢

Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu

openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)

有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器

实现一个博客系统----使用模板引擎技术

Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products

周大福践行「百周年承诺」,真诚服务推动绿色环保

Countermeasures of enterprise supply chain management system in UCA Era

Thymeleaf 模板的创建与使用

Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading

世界环境日 | 周大福用心服务推动减碳环保
随机推荐
anaconda使用中科大源
R语言ggplot2可视化:使用ggplot2可视化散点图、使用labs参数自定义X轴的轴标签文本(customize X axis labels)
Thymeleaf common functions
Structure - C language
LeetCode_ 67 (binary sum)
R语言dplyr包select函数、group_by函数、mutate函数、cumsum函数计算dataframe分组数据中指定数值变量的累加值、并生成累加数据列
【招聘岗位】软件工程师(全栈)- 公共安全方向
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
Judge whether the variable is an array
R Language ggplot2 Visualization: visualize linegraph, using Legend in Theme function. Paramètre de position emplacement de la légende personnalisée
直播预告|如何借助自动化工具落地DevOps(文末福利)
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
APR protocol and defense
js亮瞎你眼的日期选择器
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
Enjoy what you want. Zhichuang future
mysql8.0JSON_ Instructions for using contains