当前位置:网站首页>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 :
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 :
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 !
边栏推荐
- What else do you not know about new map()
- 关于mysql中的json解析函数JSON_EXTRACT
- 调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
- Jarvis OJ Flag
- Read the basic grammar of C language in one article
- 阈值同态加密在隐私计算中的应用:解读
- How does the outer disk futures platform distinguish formal security?
- Copy mode DMA
- [Jianzhi offer] 63 Maximum profit of stock
- 【testlink】TestLink1.9.18常见问题解决方法
猜你喜欢
ECU introduction
机器学习01:绪论
The second day of learning C language for Asian people
High number | summary of calculation methods of volume of rotating body, double integral calculation of volume of rotating body
How to write a full score project document | acquisition technology
MySql 查询符合条件的最新数据行
thinkphp3.2.3
stirring! 2022 open atom global open source summit registration is hot!
机器学习编译第2讲:张量程序抽象
【Web攻防】WAF检测技术图谱
随机推荐
网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
goto Statement
Etcd 构建高可用Etcd集群
浏览器渲染原理以及重排与重绘
Embedded UC (UNIX System Advanced Programming) -1
一文了解MySQL事务隔离级别
33:第三章:开发通行证服务:16:使用Redis缓存用户信息;(以减轻数据库的压力)
激动人心!2022开放原子全球开源峰会报名火热开启!
Writing method of twig array merging
MySql 查询符合条件的最新数据行
【Web攻防】WAF检测技术图谱
How to write a full score project document | acquisition technology
深耕5G,芯讯通持续推动5G应用百花齐放
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
Wsl2.0 installation
【性能测试】jmeter+Grafana+influxdb部署实战
Jarvis OJ simple network management protocol
阈值同态加密在隐私计算中的应用:解读