Article Directory
1. Screening conditions
1.1, comparison operators
equal:=(注意!不是==) |
greater or equal to:>= |
null:is NULL |
---|---|---|
not equal to:!=或<> |
less than:< |
non empty: is NOT NULL |
more than the:> |
less than or equal to:<= |
1.2, logical operators
AND: and
OR: or
NOT:not
1.3, sorting
Positive order (descending order):order by 字段
Conversely (descending order):order by 字段 desc
For example:select * from 表名 order by 字段
1.4, Restrictions
- limit 数字
: The limit condition can only output the first few lines
- limit num1, num2
: Output num2 rows of data from the next position of num1
1.5, deduplication
- distinct
: Do not display duplicate data when searching
For example:select distinct * from 表名
1.6, fuzzy query
- any number of characters:like '%'
For example:select * from 表名 where 字段 like '李%'
- any one character:like '_'
For example:select * from 表名 where 字段 like '李__'
1.7, range query
- Numeric query:between num1 and num2
For example:select * from 表名 where 字段 between num1 and num2
-Interval return: in (条件,条件)
, only filter the data that matches the brackets
For example:select * from 表名 where 字段 in(条件,条件...)
2. Aggregation and grouping (key and difficult points)
2.1, commonly used aggregation functions
Number of statistics: count (column) | Summing: sum(column) |
---|---|
**maximum value: **max(column) | **average:**avg(column) |
**Minimum: **min(column) | **List all values of the field: **group_concat(column) |
Example: select count(*) as group_counts from 表名 group_by 字段
: First group the table by field, calculate the number of each group, and finally store it in the group_counts field
:select 字段 from 表名 group by 字段
List the contents of the field
2.2, group query (group by)
2.3, aggregation screening
- having
: Equivalent to where, you can filter the filtered table again
:where和having的区别
where is to participate in the screening when viewing, and having is to be executed after the first screening, which is equivalent to re-screening the already-filtered tables.
- as
: You can name the aggregate function and use it later
select * from (select * from students order by age) as e;
# (select * from students order by age) as e 这一步就是期别名为e
Three, subquery (understand)
3.1, leave the result of a query for the next query ( select 中嵌套 select
)
Requirements:
1): Nested inside the query
2): Must always appear in parentheses (data in parentheses must be a single data when not aliased)
Example: select count(*) from grades where subject_number=(select number from subjects where title='Python1');
: query subject_number is equal to the data in the following subquery (the data in the subquery must be a definite unique number)
Four, connection query
4.1, inner join (inner join)
-无条件连接
:
Unconditional inner join, also known as cross join/Cartesian join
Each item in the first table will be combined with each item in the other table in turn
Example:MySQL> select * from 表名 join 表名
-有条件内连接
:
On the basis of unconditional connection, add an on clause
When connecting, filter out those meaningful records for combination
Example: MySQL> select * from 表名 join 表名 on 表名.字段 = 表名.字段
If you don’t want to display repeated data, change * to the desired data, and change the repeated to condition.field
4.2, outer join (left/right join)
- 左外链接
: (Based on the left table)
When two tables are linked, when the connection conditions do not match
Leave the data in the left table, while the data in the right table is filled with NULL
example:MySQL> select * from 表名 left join 表名 on 表名.字段 = 表名.字段
- 右外连接
(Based on the table on the right)
…