当前位置:网站首页>Interview shock 62: what are the precautions for group by?
Interview shock 62: what are the precautions for group by?
2022-07-05 14:37:00 【Wang Lei】
Be careful : The following content of this article is based on MySQL 5.7 InnoDB Database engine .
1.group by Can't add after where
stay 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 alone
having and group by Can be used alone , The following query is shown : 
3.having and group by You can use aliases
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 : 
Why? where You can't use aliases ? by having But you can ?
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 :https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html
PS:group by Be able to use the principle and having similar .
summary
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 .
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number .
official account :Java Analysis of the real interview questions
Interview collection :https://gitee.com/mydb/interview
边栏推荐
- [summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
- Thymeleaf th:with use of local variables
- Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
- 美国费城发生“安全事故” 2名警察遭枪杀
- How to choose the appropriate certificate brand when applying for code signing certificate?
- CPU设计实战-第四章实践任务三用前递技术解决相关引发的冲突
- Matrix chain multiplication dynamic programming example
- 想进阿里必须啃透的12道MySQL面试题
- APR protocol and defense
猜你喜欢

Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发

【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !

分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
![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]

Security analysis of Web Architecture

Pointer operation - C language

Under the crisis of enterprise development, is digital transformation the future savior of enterprises

Share 20 strange JS expressions and see how many correct answers you can get

有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器

【华为机试真题详解】字符统计及重排
随机推荐
TS所有dom元素的类型声明
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
一网打尽异步神器CompletableFuture
Lepton 无损压缩原理及性能分析
强联通分量
分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
注意!软件供应链安全挑战持续升级
【招聘岗位】软件工程师(全栈)- 公共安全方向
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
一键更改多个文件名字
Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
危机重重下的企业发展,数字化转型到底是不是企业未来救星
长列表优化虚拟滚动
实现一个博客系统----使用模板引擎技术
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
04_ Use of solrj7.3 of solr7.3
The function of qualifier in C language