当前位置:网站首页>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
边栏推荐
- CPU设计实战-第四章实践任务三用前递技术解决相关引发的冲突
- mysql8.0JSON_ Instructions for using contains
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
- FR练习题目---简单题
- 日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
- 动态规划
- [learning notes] stage test 1
- 做自媒體視頻二次剪輯,怎樣剪輯不算侵權
- Mysql database installation tutorial under Linux
- 黑马程序员-软件测试-10阶段2-linux和数据库-44-57为什么学习数据库,数据库分类关系型数据库的说明Navicat操作数据的说明,Navicat操作数据库连接说明,Navicat的基本使用,
猜你喜欢

分享 12 个最常用的正则表达式,能解决你大部分问题

729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」

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

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

Principle and performance analysis of lepton lossless compression

Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!

Thymeleaf 使用后台自定义工具类处理文本

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

世界环境日 | 周大福用心服务推动减碳环保

ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
随机推荐
Sharing the 12 most commonly used regular expressions can solve most of your problems
Un week - end heureux
[detailed explanation of Huawei machine test] character statistics and rearrangement
启牛学堂班主任给的证券账户安全吗?能开户吗?
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Strong connection component
[detailed explanation of Huawei machine test] happy weekend
Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management
How to protect user privacy without password authentication?
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
leetcode:881. 救生艇
Share 20 strange JS expressions and see how many correct answers you can get
详解Vue适时清理keepalive缓存方案
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
[C question set] of Ⅷ
R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
【招聘岗位】软件工程师(全栈)- 公共安全方向
anaconda使用中科大源
C语言中限定符的作用
[learning notes] stage test 1