当前位置:网站首页>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 :

  1. select
  2. from
  3. where
  4. group by
  5. having
  6. order by
  7. 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 : image.png The execution of the syntax sequence is and MySQL Of select Statement execution order related ,select The order of execution is as follows :

  1. from Stage
  2. where Stage
  3. group Stage
  4. having Stage
  5. select Stage
  6. order by Stage
  7. 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 : image.png

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(250comment ' full name ',
    math decimal(4,1comment ' Math scores ',
    chinese decimal(4,1comment ' 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 : image.png When we use the total score alias total Respectively in where and having When used in , The query results are as follows : image.png 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 : image.png

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 :

  1. from Stage
  2. where Stage
  3. group Stage
  4. having Stage
  5. select Stage
  6. order by Stage
  7. 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 : image.png 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

原网站

版权声明
本文为[Wang Lei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051431476625.html