1. The complete syntax of select
select field1, field2, ...,fieldN(*表示所有字段)
from tableName
[where condition]
[group by {field | expr | position} [having condition]
[order by {field | expr | position} [asc | desc]
[limit offset,count];
As shown in the figure below, query the name and age fields of all records in the contacts table.
2. Single condition query
In SQL, insert, update, delete and select can be followed by a where clause to insert, modify, delete or query records with specified conditions.
Operator | description |
---|---|
= | equal |
!= | not equal to |
> | more than the |
< | Less than |
>= | greater or equal to |
<= | Less than or equal to |
between and | Select the data range between two values; in MySQL, it is equivalent to >= and <= |
select field
from tableName
where field 运算符 value;
As shown in the figure below, query the records whose age is greater than or equal to 28 in the contacts table.
3. Multi-condition query
In the where clause, use and, or to combine two or more filter conditions.
Operator | description |
---|---|
and | Indicates that the conditions on the left and right sides are met at the same time |
or | It means that as long as one condition is satisfied on both sides |
select field
from tableName
where condition1 and condition2 or condition3;
As shown in the figure below, query the records with age equal to 27 or 29 in the contacts table.
4. The use of
operator in Operator in can filter the value of a certain field in the where clause.
select field
from tableName
where field in (value1, value2, ...valueN);
As shown in the figure below, query the records whose age is equal to 27 and 28 in the contacts table.
5. The use of
operator like The operator like can fuzzy match the value of a string in the where clause.
select field
from tableName
where field like ‘%value%’;
As shown in the figure below, query the records whose name field in the contacts table contains "eve".
6. Use distinct to remove duplicate data.
distinct is used to return the unique value of the column in the query (de- duplication ), and supports single or multiple columns.
select distinct field1, field2,...,fieldN
from tableName;
As shown in the figure below, de-duplicate the name field in the contacts table.
7. Use order by to sort
select field1, field2,...,fieldN
from tableName1, tableName2
order by field1, field2[asc|desc]
asc means sort in ascending order, desc means sort in descending order.
By default, the columns are sorted in ascending order.
As shown in the figure below, query all records in the contacts table and sort them in descending order by age.
8. Use limit for paging
select field1, field2,...,fieldN
from tableName1, tableName2
limit [offset,] count
offset specifies the offset of the first row to be returned. The offset of the first line is 0, not 1.
count specifies the maximum number of rows to be returned.
The paging formula of limit: limit (page-1)*count, count.
As shown in the figure below, in the contacts table, the index starts from 0 and the total number is 2 records.
9. Use group by and having for grouping query
group by means that the data is grouped according to a certain rule. It must be used in conjunction with the aggregation function. After the data is grouped, operations such as count, sum, avg, max, and min can be performed. The reason for adding the having clause is that the where keyword cannot be used with aggregate functions. The having clause can filter each group of data after grouping.
select field, aggregate_function(field)
from tableName
group by field
having aggregate_function(field) operator value
aggregate_function represents an aggregate function.
Group by can group one or more columns.
As shown in the figure below, the contacts table is grouped by age, and after grouping, the number of groups is greater than or equal to 2.
10. The use of
group_concat group_concat is used in conjunction with group by to concatenate the values of a certain column according to the specified separator. The MySQL default separator is a comma.
select field, group_concat(field1)
from tableName
group by field
As shown in the figure below, group the contacts table by age and display the names of the members of the group.
11. Subquery in
operator in, which allows us to filter multiple values of a field in the where clause. If the value behind the operator in is derived from a certain query result instead of several specified values, then a subquery is needed. Subquery is also called inner query or nested query, that is, the query statement is embedded in the where clause of the SQL query.
select field from tableName
where field in(select field1 from tableName1 [WHERE]);
12. The subquery exists
exists is a Boolean operator used to test whether the internal query returns any rows in the subquery. Put the data of the main query into the subquery for conditional verification, and determine whether the data result of the main query is retained according to the verification result (true or false).
select field
from tableName
where exists(select * from tableName1 where condition);