1. Conditions
- Use the where clause to filter the data in the table, and the rows with the result true will appear in the result set
- The syntax is as follows:
select * from 表名 where 条件;
comparison operator
- equals =
- greater than>
- Greater than or equal to >=
- less than <
- Less than or equal to <=
- Not equal to != or <>
- Search for students with numbers greater than 3
select * from students where id>3;
- Query subjects whose number is not greater than 4
select * from subjects where id<=4;
- Search for students whose name is not "Huang Rong"
select * from students where sname!='黄蓉';
- Query students who have not been deleted
select * from students where isdelete=0;
Logical Operators
- and
- or
- not
- Query female students whose number is greater than 3
select * from students where id>3 and gender=0;
- Search for students whose ID is less than 4 or has not been deleted
select * from students where id<4 or isdelete=0;
fuzzy query
- like
- % means any number of arbitrary characters
- _ means an arbitrary character
- Find students with the last name Huang
select * from students where sname like '黄%';
- Find students with the last name Huang and the first name is one character
select * from students where sname like '黄_';
- Find students with the last name Huang or Jing
select * from students where sname like '黄%' or sname like '%靖%';
range query
- in means in a non-contiguous range
- Enquiry for students whose numbers are 1 or 3 or 8
select * from students where id in(1,3,8);
- between ... and ... means in a continuous range
- Query students are 3 to 8 students
select * from students where id between 3 and 8;
- Check if the student is a boy from 3 to 8
select * from students where id between 3 and 8 and gender=1;
Empty judgment
- Note: null is not the same as ''
- Empty is null
- Check for students who do not have an address
select * from students where hometown is null;
- is not null
- Find students with addresses
select * from students where hometown is not null;
- Find the girl who filled in the address
select * from students where hometown is not null and gender=0;
priority
- parentheses, not, comparison operators, logical operators
- And is calculated before or. If it appears at the same time and you want to calculate or first, you need to use it in combination with ()
2. Aggregation
- In order to get statistics quickly, 5 aggregation functions are provided
- count(*) means to calculate the total number of rows, write asterisk and column name in parentheses, the result is the same
- Query the total number of students
select count(*) from students;
- max(column) means to find the maximum value of this column
- Query the maximum number of girls
select max(id) from students where gender=0;
- min(column) means to find the minimum value of this column
- Query the minimum number of undeleted students
select min(id) from students where isdelete=0;
- sum(column) means to find the sum of this column
- After querying the boy's number
select sum(id) from students where gender=1;
- avg(column) means to find the average of this column
- Query the average number of undeleted girls
select avg(id) from students where isdelete=0 and gender=0;
3. Grouping
- Group by field, indicating that data with the same field will be put into a group
- After grouping, only the same data column can be queried, and the different data columns cannot appear in the result set
- The grouped data can be counted and aggregated
- grammar:
select 列1,列2,聚合... from 表名 group by 列1,列2,列3...
- Query the total number of boys and girls
select gender as 性别,count(*)
from students
group by gender;
- Query the number of people in each city
select hometown as 家乡,count(*)
from students
group by hometown;
Data filtering after grouping
select 列1,列2,聚合... from 表名
group by 列1,列2,列3...
having 列1,...聚合...
- The conditional operator after having is the same as where
- Query the total number of boys
方案一
select count(*)
from students
where gender=1;
-----------------------------------
方案二:
select gender as 性别,count(*)
from students
group by gender
having gender=1;
Contrast where and having
- where is to filter the data of the table specified after from, which belongs to the filtering of the original data
- having is to filter the results of group by
4. Sort
- Data can be sorted for easy viewing
- grammar:
select * from 表名
order by 列1 asc|desc,列2 asc|desc,...
- Sort the row data according to column 1, if some rows and columns 1 have the same value, then sort according to column 2, and so on
- By default, the column values are sorted from small to large
- asc is arranged from small to large, that is, in ascending order
- desc sort from big to small, that is, descending
- Query the information of male students that have not been deleted, in descending order by student number
select * from students
where gender=1 and isdelete=0
order by id desc;
- Query the information of undeleted accounts, in ascending order by name
select * from subject
where isdelete=0
order by stitle;
5. Get some rows
- When the amount of data is too large, it is very troublesome to view the data in one page
- grammar
select * from 表名
limit start,count
- From start, get count pieces of data
- start index starts from 0
Example: Pagination
- Known: m pieces of data are displayed on each page, and the nth page is currently displayed
- Find the total number of pages: this logic will be implemented in python later
- The total number of queries p1
- Divide p1 by m to get p2
- If divisible then p2 is the total number of pages
- If not divisible, p2+1 is the total number of pages
- Find the data of the nth page
select * from students
where isdelete=0
limit (n-1)*m,m
6. Summary
- Complete select statement
select distinct *
from 表名
where ....
group by ... having ...
order by ...
limit star,count
- The order of execution is:
- from table name
- where ....
- group by ...
- select distinct *
- having ...
- order by ...
- limit star,count
- In actual use, it is only a combination of some parts of the statement, not all of them