当前位置:网站首页>面试突击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面试真题解析
边栏推荐
- The simplest way to open more functions without certificates
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- Redis如何实现多可用区?
- Thymeleaf th:with局部变量的使用
- 家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
- 强联通分量
- 直播预告|如何借助自动化工具落地DevOps(文末福利)
- Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
- R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
- How to deeply understand the design idea of "finite state machine"?
猜你喜欢

家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级

Security analysis of Web Architecture

无密码身份验证如何保障用户隐私安全?
![[learning notes] stage test 1](/img/22/ad16375d8d1510c2ec75c56403a8bf.png)
[learning notes] stage test 1

LeetCode_ 2 (add two numbers)

How can non-technical departments participate in Devops?

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

How to deeply understand the design idea of "finite state machine"?

Thymeleaf th:with局部变量的使用

Thymeleaf th:with use of local variables
随机推荐
选择排序和冒泡排序
Use the word "new" to attract curious people
Why do mechanical engineers I know complain about low wages?
直播预告|如何借助自动化工具落地DevOps(文末福利)
【NVMe2.0b 14-9】NVMe SR-IOV
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
Show strength. In this way, the mobile phone will not be difficult to move forward
申请代码签名证书时如何选择合适的证书品牌?
anaconda使用中科大源
超级哇塞的快排,你值得学会!
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
R语言ggplot2可视化条形图:通过双色渐变配色颜色主题可视化条形图、为每个条形添加标签文本(geom_text函数)
开挖财上的证券账户可以吗?安全吗?
Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
想进阿里必须啃透的12道MySQL面试题
How to make a second clip of our media video without infringement
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute