当前位置:网站首页>面试突击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面试真题解析
边栏推荐
- Thymeleaf common functions
- Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
- C language -- structure and function
- How to open an account of qiniu securities? Is it safe to open an account?
- Topology visual drawing engine
- 网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
- Strong connection component
- After the microservice project is deployed, static resources and files uploaded to upload cannot be accessed. Solution
- 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
- 04_ Use of solrj7.3 of solr7.3
猜你喜欢

leetcode:881. 救生艇

Redis如何实现多可用区?

Loop invariant

CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion

Share 20 strange JS expressions and see how many correct answers you can get

There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba

Thymeleaf th:with局部变量的使用

Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)

非技术部门,如何参与 DevOps?

Thymeleaf th:with use of local variables
随机推荐
The simplest way to open more functions without certificates
外盘入金都不是对公转吗,那怎么保障安全?
Longest common subsequence dynamic programming
启牛学堂班主任给的证券账户安全吗?能开户吗?
总量分析 核算方法和势方法 - 分摊分析
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
【学习笔记】阶段测试1
超级哇塞的快排,你值得学会!
Thymeleaf 使用后台自定义工具类处理文本
Penetration testing methodology
美国费城发生“安全事故” 2名警察遭枪杀
开挖财上的证券账户可以吗?安全吗?
FR练习题目---综合题
Thymeleaf th:with use of local variables
anaconda使用中科大源
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
强联通分量
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
选择排序和冒泡排序
Webrtc learning (II)