当前位置:网站首页>面试突击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面试真题解析
边栏推荐
- How does redis implement multiple zones?
- 分享 12 个最常用的正则表达式,能解决你大部分问题
- 01. Solr7.3.1 deployment and configuration of jetty under win10 platform
- Introduction, installation, introduction and detailed introduction to postman!
- Lepton 无损压缩原理及性能分析
- 动态规划
- 不相交集
- R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
- CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
- js亮瞎你眼的日期选择器
猜你喜欢

【NVMe2.0b 14-9】NVMe SR-IOV

【NVMe2.0b 14-9】NVMe SR-IOV

Redis如何实现多可用区?

Thymeleaf th:with use of local variables

ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
![[learning notes] stage test 1](/img/22/ad16375d8d1510c2ec75c56403a8bf.png)
[learning notes] stage test 1

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

Thymeleaf 模板的创建与使用

想进阿里必须啃透的12道MySQL面试题

非技术部门,如何参与 DevOps?
随机推荐
区间 - 左闭右开
Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
03_ Dataimport of Solr
be careful! Software supply chain security challenges continue to escalate
详解Vue适时清理keepalive缓存方案
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
Countermeasures of enterprise supply chain management system in UCA Era
乌卡时代下,企业供应链管理体系的应对策略
Is it OK to open the securities account on the excavation finance? Is it safe?
Thymeleaf th:with局部变量的使用
Fonctions communes de thymeleaf
R language dplyr package select function, group_ By function, mutate function and cumsum function calculate the cumulative value of the specified numerical variable in the dataframe grouping data and
What are the advantages and characteristics of SAS interface
How can non-technical departments participate in Devops?
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
【招聘岗位】软件工程师(全栈)- 公共安全方向
How to call the function mode of one hand and one machine
Redis如何实现多可用区?
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)