当前位置:网站首页>面试突击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 to open an account of qiniu securities? Is it safe to open an account?
- Longest common subsequence dynamic programming
- Assembly language
- R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the labs parameter to customize the X axis label text (customize X axis labels)
- 周大福践行「百周年承诺」,真诚服务推动绿色环保
- What are the advantages and characteristics of SAS interface
- SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
- 01. Solr7.3.1 deployment and configuration of jetty under win10 platform
- Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
猜你喜欢
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
周大福践行「百周年承诺」,真诚服务推动绿色环保
Penetration testing methodology
实现一个博客系统----使用模板引擎技术
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
微帧科技荣获全球云计算大会“云鼎奖”!
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
Security analysis of Web Architecture
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
随机推荐
MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
Topology可视化绘图引擎
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
想进阿里必须啃透的12道MySQL面试题
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
超级哇塞的快排,你值得学会!
矩阵链乘 - 动态规划实例
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
Use the word "new" to attract curious people
How to introduce devsecops into enterprises?
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)、使用shadow_mark函数为动画添加静态散点图作为动画背景
The simplest way to open more functions without certificates
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
乌卡时代下,企业供应链管理体系的应对策略
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
Isn't it right to put money into the external market? How can we ensure safety?
[learning notes] connectivity and circuit of graph
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向