1. Basic Grammar
- Format: select * from table name
- Note: The
from keyword is followed by the table name, indicating that the data comes from
the column name in the table written after the select of this table . If * indicates that all the columns
in the table are displayed in the column name part of the select after the select, you can use as The column name is aliased, and this alias is displayed in the result set.
If you want to query multiple columns, use commas to separate - Example:
select name, age from student; (search for specific fields)
select name as n, age from student; (from alias)
2. Eliminate duplicate lines
- Use distinct before the column after select to eliminate duplicate rows
- 示例:
select gender from student;
select distinct gender from student;
3. Conditional query
- Syntax: select * from table name where condition
- Comparison operator
equal =
greater than>
less than <
greater than or equal to> =
less than or equal to <=
not equal to! = Or <> (this is basically not used)
eg: query all data with id value greater than 8
select * from student where id> 8;
- Logical operator
and and
or or
not non-
eg: query female students with id value greater than 7
select * from student where id> 7 and gender = 0;
- Fuzzy query
like
% means any number of characters
eg: query classmates with surname Feng
select * from student where name like '冯%';
_ means an arbitrary character
eg: query classmates with surname Feng
select * from student where name like 'Feng_ ';
- Range query
in means that in a non-continuous range
eg: the query number is 8, 10, 12 students
select * from student where id in (8,10,12);
between ... and ... means in a continuous Within the range of
eg: query the students with the number 6 ~ 8
select * from student where id between 6 and 8;
- Null judgment
Note: null is different from ''
Judging null: is null
judging non-empty: is not null
eg: querying students with no address
Example: select * from student where address is null;
eg: querying students with address
Example: select * from student where address is not null;
- Priority
Parentheses, not, comparison operators, logical operators
and have higher priority than or, if they appear at the same time and you want to select or first, you need to use parentheses
4. Aggregation
In order to get statistics quickly, 5 aggregate functions are provided
- count (*) means to calculate the total number of rows. You can write * and column names in parentheses.
Requirement: Query the total number of students.
Example: select count (*) from student; == select count (id) from student;
- max (column) means to find the maximum value of this column.
Requirement: query the maximum value of the girl number
Example: select max (id) from student where gender = 0;
- min (column) means to find the minimum value of this column.
Requirement: Query the minimum value of the girl number
Example: select min (id) from student where gender = 0;
- Sum (column) means to find the sum of this column.
Demand: Query the sum of the ages of all girls.
Example: select sum (age) from student where gender = 0;
- Avg (column) means to find the average value of this column.
Requirement: query the average value of the ages of all girls.
Example: select avg (age) from student where gender = 0;
5. Grouping
- Grouping by field means that the same data in this field will be put into a collection.
- After grouping, only the same data columns can be queried, and the data columns with differences cannot be displayed in the result set
- You can perform statistics on the grouped data and perform aggregation operations
- Syntax: select column 1, column 2, aggregation ... from table name group by column 1, column 2, column 3
- Demand: check the total number of boys and girls
- 示例:
select gender,count(*) from student group by gender;
select name,gender,count(*) from student group by gender,age;
- Data filtering after grouping: select column 1, column 2, aggregation ... from table name group by column 1, column 2, column 3, ... having column 1, ... Aggregation ...
Example: select gender, count (*) from student group by gender having gender;
(Find out the data with gender because the girl default is 0, so only the boys are filtered 1 True)
select gender, count (* ) from student group by gender having gender = 0; - The difference between where and having
where is to filter the table specified after from, belongs to the filtering of the original data
having is to filter the results of group by
6. Sorting
- Syntax: select * from table name order by column 1 asc | desc, column 2 asc | desc, ......
- Description:
Sort the data according to column 1, if some of the values of column 1 are the same, then sort according to column 2 The
default is to sort from small to large
asc descending order descending order descending
order
- Example:
sort by age
select * from student order by age;
sort the data that has not been deleted by age
select * from student where isDelete = 0 order by age; the
same age in the above table is sorted in ascending order by id value, then we will change Sort in descending order
7. Pagination
- Syntax: select * from table name limit start, count;
- Note: If there is a lot of data in the table, you can page through the
start index starting from 0
- Example:
select * from student limit 0,3;
select * from student limit 3,3;
select * from student where gender = 1 limit 3,3; This article looks at the 3rd, 4th, and 5th data of male gender. Don't look at girls