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.group by or having Can be used alone
having and group by Can be used alone , The following query is shown :
3.group by and having 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 aliases are available ?
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
Interview shock 62:group by What are the precautions ? More articles about
- 《【 Interview shock 】— Redis piece 》--Redis What are the data types ? Which scenarios are suitable for use ?
Can stick to what others can't stick to , To have what others can't have . Focus on programming Avenue official account , Let's stick to what we think , Growing up together !! <[ Interview shock ]— Redis piece >--Redis What are the data types ? Which scenarios are used respectively ...
- 《【 Interview shock 】— Redis piece 》-- Redis Do you understand the thread model of ? Why is single thread efficiency so high ?
Can stick to what others can't stick to , To have what no one else has . Focus on programming Avenue official account , Let's stick to what we think , Growing up together !! <[ Interview shock ]— Redis piece >-- Redis Do you understand the thread model of ? Why is single thread efficiency still low ...
- 《【 Interview shock 】— Redis piece 》-- Redis Master-slave replication of ? Sentinel mechanism ?
Can stick to what others can't stick to , To have what no one else has . Focus on the official account of the upper left corner of programming Avenue , Let's stick to what we think , Growing up together !! <[ Interview shock ]— Redis piece >-- Redis Master-slave replication of ? Sentinel mechanism ? In this ...
- 《【 Interview shock 】— Redis piece 》-- Redis Sentry principle and persistence mechanism
Can stick to what others can't stick to , To have what no one else has . Focus on programming Avenue official account , Let's stick to what we think , Growing up together !! <[ Interview shock ]— Redis piece >-- Redis Sentry principle and persistence mechanism In this series , ...
- 《【 Interview shock 】— Redis piece 》--Redis Cluster And common problems of cache usage and architecture design
Can stick to what others can't stick to , To have what no one else has . Focus on programming Avenue official account , Let's stick to what we think , Growing up together !! <[ Interview shock ]— Redis piece >--Redis Cluster And cache usage and architecture design ...
- Oracle DBA Interview questions
One copy ORACLE DBA Interview questions One :SQL tuning class 1: List several ways to connect tables answer : Oracle The multi table join algorithm of is Nest Loop.Sort Merge and Hash Join Three broad categories , Each category can be ...
- 【 Interview shock 】-Redis Frequently asked questions ( One )
Introduce :Redis Is an open source use ANSI C Language writing . comply with BSD agreement . Support network . Log type that can be memory based or persistent .Key-Value database , And provide multilingual API Non relational database . Traditional data ...
- 【 Interview shock 】- Java Interview general rules
Java Basics 1.Map.Set.List Set differences and contact details 2.HashSet Class is how to add elements to ensure no repetition 3.HashMap Is it thread safe , Why not thread safe ( It is best to draw a picture to illustrate the uneasiness in a multithreaded environment ...
- Redis How expired elements in are handled ? video + Here you are —— Interview shock 002 period
In this paper, interview questions 「Redis How expired elements in are handled ?」 As a starting point , Use video plus graphics to talk with you Redis Knowledge points about the processing of expired elements . Knowledge points involved What are the expiration delete policies ? What are the advantages and disadvantages of these expiration policies ...
- Use Redis How to query people nearby ?「 Video version 」—— Interview shock 003 period
Interview questions Redis How to query people nearby ? Knowledge points involved Redis How to operate location information in ? GEO How is the underlying implementation ? How to query people nearby in the program ? What problems should be paid attention to in actual use ? Video answers Video address ...
Random recommendation
- Simple Java Class and XML Conversion between
Needed jar package :xmlpull_1_0_5.jar,xstream-1.4.1.jar) 1. Tool class XstreamUtil package com.learn.util; import com.tho ...
- bzoj2792
First of all, I think the answer is two points , Set to lim This problem is difficult to judge , Let's change a number into 0 Conditions First, make the sequence meet the adjacent difference <=lim, This can be scanned one time in the front and one time in the back Then we have to deal with changing a number into 0 Cost of modification When i Turn into 0 ...
- chgrp、chown、chmod command
Explain the content : The meaning of authority code is as follows : r: Permission to read and write directories or files read w : Permission to write to a directory or file write x : Permission to execute directories or files -: Permission to delete directories or files s: Special privileges , Change directories or files ...
- AzCopy – Cross account replication Blob
You can always go from aka.ms/AzCopy Download the latest version . last year 4 New features in the version released in September Support cross account replication Blob:AzCopy Allows you to copy within the same storage account or between different storage accounts Blob( About cross account B ...
- HDU 2852 KiKi's K-Number
Weight line tree #include <cstdio> #include <cstring> const int N=200000,M=220000; int k,q,x,y,sum[ ...
- md5 Certificate in window2012 Cannot access
Before that window 2008 Use makecert The generated certificate , Deploy to window 2012 after , Only found IE Access to , however Firefox and chrome Neither. .Firefox have access to about:config ...
- Time series database InfluxDB Installation and use
Time series database InfluxDB Installation and use 1 Installation configuration install wget https://dl.influxdata.com/influxdb/releases/influxdb-1.3.1.x86_64. ...
- gitbash Use git Preparation of orders
1.git download 2.git clone http://git.missfresh.cn/... Configure user name and password 3.git Common command abbreviation configuration git config --global alias.st ...
- Follow the book every day Mysql
Learn more Mysql Operation CRUD,create,retrieve,update,delete
- PHP Development :Eclipse Version environment configuration
Software : 1.eclipse php Version download address :http://www.eclipse.org/downloads/packages/eclipse-php-developers/heliosr 2.A ...