当前位置:网站首页>面试突击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面试真题解析
边栏推荐
猜你喜欢

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

网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理

Thymeleaf 模板的创建与使用

无密码身份验证如何保障用户隐私安全?

申请代码签名证书时如何选择合适的证书品牌?

leetcode:881. 救生艇

How to introduce devsecops into enterprises?

Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
![Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]](/img/c2/a5f5fe17a6bd1f6f9df828ddd224d6.png)
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]

FR练习题目---综合题
随机推荐
How to introduce devsecops into enterprises?
mysql8.0JSON_ Instructions for using contains
FR练习题目---综合题
Assembly language
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
最长公共子序列 - 动态规划
详解Vue适时清理keepalive缓存方案
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
开挖财上的证券账户可以吗?安全吗?
微帧科技荣获全球云计算大会“云鼎奖”!
世界环境日 | 周大福用心服务推动减碳环保
R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to each vari
Isn't it right to put money into the external market? How can we ensure safety?
Thymeleaf th:with局部变量的使用
【NVMe2.0b 14-9】NVMe SR-IOV
Don't be unconvinced. Mobile phone function upgrade is strong
Topology可视化绘图引擎
[learning notes] connectivity and circuit of graph
Topology visual drawing engine