当前位置:网站首页> MYSQL group by 有哪些注意事项
MYSQL group by 有哪些注意事项
2022-07-05 16:42:00 【1024问】
1.group by 后面不能加 where
2.having 或 group by 可单独使用
3.having 和 group by 可使用别名
为什么where不能用别名?为having却可以?
总结
前言:
注意:本文以下内容基于 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 可以单独使用,如下查询所示:
**当 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 中不能使用别名,这和 MySQL 语句执行顺序有关,MySQL 语句执行顺序如下:
from 阶段
where 阶段
group 阶段
having 阶段
select 阶段
order by 阶段
limit 阶段
也就是说,**在执行 where 查询时,select 还没执行,因此在 where 中想要使用还未执行的 select 中的别名是不行的。**那从上面的执行顺序可以看到,having 执行也在 select 之前,为什么它就可以使用 select 中的别名呢?
这是因为 MySQL 在 5.7.5 之后做了扩展,允许在 having 中使用别名,官方文档中有相应的说明,
如下图所示:
MySQL 官方文档地址:
总结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 不能使用别名。
到此这篇关于MYSQL group by 有哪些注意事项?的文章就介绍到这了,更多相关MYSQL group by内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- 拷贝方式之DMA
- Yarn common commands
- 编译libssh2报错找不到openssl
- 通过proc接口调试内核代码
- Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
- IDC报告:腾讯云数据库稳居关系型数据库市场TOP 2!
- Cs231n notes (bottom) - applicable to 0 Foundation
- 【testlink】TestLink1.9.18常见问题解决方法
- Embedded-c language-6
- Combined use of vant popup+ other components and pit avoidance Guide
猜你喜欢
Embedded UC (UNIX System Advanced Programming) -1
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
CMake教程Step2(添加库)
ternary operator
Deep learning plus
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
Summary of PHP pseudo protocol of cisp-pte
深潜Kotlin协程(二十一):Flow 生命周期函数
winedt常用快捷键 修改快捷键latex编译按钮
Jarvis OJ shell traffic analysis
随机推荐
【Web攻防】WAF检测技术图谱
[Jianzhi offer] 66 Build product array
[Web attack and Defense] WAF detection technology map
Embedded UC (UNIX System Advanced Programming) -3
What else do you not know about new map()
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
通过proc接口调试内核代码
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
项目引入jar从私服Nexus 拉去遇到的一个问题
Etcd 构建高可用Etcd集群
Embedded-c Language-4
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
张平安:加快云上数字创新,共建产业智慧生态
【剑指 Offer】61. 扑克牌中的顺子
深耕5G,芯讯通持续推动5G应用百花齐放
Deeply cultivate 5g, and smart core continues to promote 5g applications
菜刀,蚁剑,冰蝎,哥斯拉的流量特征
CMake教程Step2(添加库)