当前位置:网站首页>What are the precautions for MySQL group by
What are the precautions for MySQL group by
2022-07-05 17:14:00 【1024 questions】
1.group by Can't add after where
2.having or group by Can be used alone
3.having and group by You can use aliases
Why? where You can't use aliases ? by having But you can ?
summary
Preface :
Be careful : The following content of this article is based on MySQL 5.7 InnoDB Database engine .
1.group by Can't add after wherestay MySQL in , be-all SQL The query syntax should follow the following syntax order :
select
from
where
group by
having
order by
limit
The above grammatical order is not interchangeable , Otherwise, the report will be wrong . For example, we can't group by Then add where Query statement , Otherwise, the following error will occur :
The execution of the syntax sequence is and MySQL Of select Statement execution order related ,select The order of execution is as follows :
from Stage
where Stage
group Stage
having Stage
select Stage
order by Stage
limit Stage
Be careful : among select A special , When writing query statements , Write it first , Other grammatical order shall be consistent with the execution order .
2.having or group by Can be used alonehaving and group by Can be used alone , The following query is shown :
** When having When used alone , Its role and where similar , But there are subtle differences .** For example where Alias cannot be used in , but having and group by But it can be another name . Let's create a test table to demonstrate , Build table SQL as follows :
drop table if exists student_score;create table student_score( id int primary key auto_increment comment ' Primary key ', name varchar(250) comment ' full name ', math decimal(4,1) comment ' Math scores ', chinese decimal(4,1) comment ' Chinese achievement ');insert into student_score(name,math,chinese) values(' Zhang San ',50,50),(' Li Si ',80,80),(' Wang Wu ',90,90);
The data in the table is shown in the figure below :
When we use the total score alias total Respectively in where and having When used in , The query results are as follows :
As can be seen from the above results ,having Queries can use select Alias in , and where Cannot use alias . except having You can use something other than an alias ,group by You can also use aliases ,
As shown in the figure below :
where Alias cannot be used in , This sum MySQL Statement execution order ,MySQL The execution order of the statements is as follows :
from Stage
where Stage
group Stage
having Stage
select Stage
order by Stage
limit Stage
in other words ,** In execution where When inquiring ,select Not yet implemented , So in where You want to use a that has not yet been executed select Aliases in are not acceptable .** From the above execution sequence, you can see ,having Execution is also select Before , Why can it be used select The alias in ?
This is because MySQL stay 5.7.5 After that, it was extended , Allow in having Use aliases , There are corresponding instructions in the official documents ,
As shown in the figure below :
MySQL Official document address :
summaryPS:group by Be able to use the principle and having similar .
SQL Statements must be written in this order :select、from、where、group by、having、order by、limit. among having or group by Can be used alone , And in MySQL 5.7.5 after ,group by and having You can use alias queries , but where Cannot use alias .
This is about MYSQL group by What are the precautions ? This is the end of the article , More about MYSQL group by Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- Learnopongl notes (II) - Lighting
- SQL删除重复数据的实例教程
- Copy mode DMA
- 国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
- Wechat official account web page authorization login is so simple
- The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
- 浏览器渲染原理以及重排与重绘
- 【剑指 Offer】62. 圆圈中最后剩下的数字
- 【剑指 Offer】61. 扑克牌中的顺子
- [729. My schedule I]
猜你喜欢
URP下Alpha从Gamma空间到Linner空间转换(二)——多Alpha贴图叠加
兰空图床苹果快捷指令
Embedded UC (UNIX System Advanced Programming) -1
[729. My schedule I]
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
[729. My Schedule i]
ECU简介
【剑指 Offer】63. 股票的最大利润
Use JDBC technology and MySQL database management system to realize the function of course management, including adding, modifying, querying and deleting course information.
Etcd 构建高可用Etcd集群
随机推荐
启牛商学院股票开户安全吗?靠谱吗?
C#(Winform) 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件
CMake教程Step2(添加库)
【729. 我的日程安排表 I】
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
[Jianzhi offer] 62 The last remaining number in the circle
麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
It is forbidden to copy content JS code on the website page
Embedded UC (UNIX System Advanced Programming) -3
叩富网开期货账户安全可靠吗?怎么分辨平台是否安全?
C# TCP如何设置心跳数据包,才显得优雅呢?
【性能测试】全链路压测
easyNmon使用汇总
PHP人才招聘系统开发 源代码 招聘网站源码二次开发
什么是ROM
stirring! 2022 open atom global open source summit registration is hot!
winedt常用快捷键 修改快捷键latex编译按钮
[Jianzhi offer] 61 Shunzi in playing cards
How does the outer disk futures platform distinguish formal security?