Format: select [option clause] field expression clause [ from clause] [ where clause] [ group by clause] [ having clause] [ order by clause] [ limit clause];
Hint: The order of clauses is fixed
select option clause [ all | distinct ]
This parameter mainly affects whether the query result contains duplicate records
All: Represents keeping all records, this default option can be omitted or not written
Distinct: Represents to remove duplicate records
Format: select all|distinct field expression from...;
from clause
Format: from datasheet
The FROM clause is used to specify the data source to be used by the query
There can be multiple data tables, separated by commas, and data tables can also be aliased
If the data source is two tables, the result will be a cross-connection method, and the number of records is equivalent to the Cartesian product
The sum of the data of the two tables = the number of records in table 1 * the number of records in table 2
When writing *, the total number of field names is the sum of the number of fields of the two tables
Data tables can also be aliased, and conditions can also be added
WHERE clause
The function is to filter the data.
Format: where conditional expression
Conditional expressions use relational and logical operators
relational operator
> greater than
< less than
>= greater than or equal to
<= less than or equal to
!= does not equal
<> is not equal to
= equal to
Logical Operators
&& and 与
|| or 或
! not not
Xor XOR
Operator precedence issues
Field aliases cannot be used in where clauses
GROUP BY clause
Mainly used for grouping. Grouping functions are often used in field expressions.
Format: group by field 1 [asc|desc], field 2 [asc|desc],
Sorting is not written, the default is asc positive order
When using count(*) and count(field name), pay attention to the problem that the value of the field is null
When count (field name) counts, records with a value of null will be ignored!
HAVING clause
It is used to filter the query results again.
It is more obvious that conditions are used again for the results of grouped queries.
When the where and group by clauses are omitted, it is not equal to having is where
You can't put where in the having position
ORDER BY clause
Mainly used to sort data.
Format: order by field 1 [asc|desc], field 2 [asc|desc],
Asc stands for positive order, it is the default option, if you don't write it, it is asc
Desc stands for reverse order
When sorting by one field, if there are records with the same sorting field, it will implicitly use the primary key for the second sorting
LIMIT clause
This clause is for pagination.
Format: limit start record position , record number
Note: If the starting record position is omitted, only the number of records is given, which means that the content is taken from the first data
Limit 10 is equivalent to limit 0, 10
Tip: The recorded position is counted from 0