Mysql tutorial (5): DQL learning
DQL Data Query Language
data query language, used to查询数据库中表的记录
1 Basic grammar
DQL query statement, the syntax structure is as follows:
SELECT
字段列表
FROM
表名列表
WHERE
条件列表
GROUP BY
分组字段列表
HAVING
分组后条件列表
ORDER BY
排序字段列表
LIMIT
分页参数
Divided into the following parts:
- Basic query (without any conditions)
- Conditional query (WHERE)
- Aggregation functions (count, max, min, avg, sum)
- Group query (group by)
- Sorting query (order by)
- Paging query (limit)
2 Basic query
Query multiple fields
SELECT 字段1, 字段2, 字段3... FROM 表名;
SELECT * FROM 表名;
Note: * represents querying all words, and should be used as little as possible in actual development (not intuitive and affects efficiency)
Field setting alias
SELECT 字段1 [ AS 别名1 ] , 字段2 [ AS 别名2 ] ... FROM 表名;
AS
Can also be omitted:
SELECT 字段1 [ 别名1 ] , 字段2 [ 别名2 ] ... FROM 表名;
Remove duplicate records
SELECT DISTINCT 字段列表 FROM 表名;
Case
A. Query the specified field name
, workno
, age
and return
select name, worknum, age from empolyee;
B. Check the employee’s ID number and create an alias
SELECT name, idcard '身份证号' FROM empolyee;
C. Query the ages of employees without duplication
SELECT DISTINCT age '年龄' FROM empolyee;
3 condition query
grammar
SELECT 字段列表 FROM 表名 WHERE 条件列表;
condition
Commonly used comparison algorithms
comparison algorithm | Function |
---|---|
> | more than the |
>= | greater or equal to |
< | less than |
<= | less than or equal to |
= | equal |
<> or != | not equal to |
BETWEEN … AND … | Within a certain range (including minimum and maximum values) |
IN(…) | The value in the list after in, multiple selection |
LIKE placeholder | Fuzzy matching (_ matches a single character, % matches any number of characters) |
IS NULL | is NULL |
Commonly used logical operators
Logical Operators | Function |
---|---|
AND 或 && | And (multiple conditions are true at the same time) |
OR 或 || | Or (any one of multiple conditions is true) |
NOT or! | No, no |
Case
Query 年龄等于21
employee information
SELECT * FROM empolyee WHERE age=21;
Query 年龄大于21
employee information
SELECT * FROM empolyee WHERE age>21;
Query 身份证号为空
employee information
SELECT * FROM empolyee WHERE idcard IS NULL;
Query 身份证号不为空
employee information
SELECT * FROM empolyee WHERE idcard IS NOT NULL;
Query 年龄不等于21
employee information
SELECT * FROM empolyee WHERE age != 21;
Query 年龄大于等于22并且小于等于24
employee information
SELECT * FROM empolyee WHERE age>=22 && age<=24;
SELECT * FROM empolyee WHERE age>=22 AND age<=24;
# BETWEEN后跟的是最小值,AND后跟的是最大值,切不可写反了
SELECT * FROM empolyee WHERE age BETWEEN 22 AND 24;
Query 年龄为24
and 性别为女
employee information
SELECT * FROM empolyee WHERE age=24 AND gender='女';
Inquiry 年龄为24
or 21
employee information
SELECT * FROM empolyee WHERE age=24 OR age=21;
SELECT * FROM empolyee WHERE age IN (21, 24);
Query 姓名为两个字
employee information
SELECT * FROM empolyee WHERE name like '__';
Query 身份证号最后一位是X
employee information
SELECT * FROM empolyee WHERE idcard LIKE '%X';
SELECT * FROM empolyee WHERE idcard LIKE '_________________X';
4 Aggregation functions
What is an aggregate function?
Take a column of data as a whole and perform vertical calculations.
Common aggregate functions
function | Function |
---|---|
count | total number |
max | maximum value |
min | minimum value |
avg | average value |
sum | Sum |
grammar
SELECT 聚合函数(字段列表) FROM 表名 ;
Note: NULL values do not participate in all aggregate function operations.
Case
A. Statistics员工数量
SELECT COUNT(*) FROM empolyee;
SELECT COUNT(idcard) FROM empolyee;
B. Statistics of employees平均年龄
SELECT AVG(age) FROM empolyee;
C. Statistics employees最大年龄
SELECT MAX(age) FROM empolyee;
D. Statistics of employees最小年龄
SELECT MIN(age) FROM empolyee;
E. Statistics of male employees年龄之和
SELECT SUM(age) FROM empolyee WHERE gender = '男';
5 Group query
grammar
SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组后过滤条件 ];
The difference between where and having
- The execution timing is different: where is filtered before grouping, if the where condition is not met, the grouping will not be participated;
- And having is to filter the results after grouping. The judgment conditions are different: where cannot judge the aggregate function, but having can.
Notice:
- After grouping, the fields queried are generally aggregate functions and grouping fields, and it is meaningless to query other fields.
- Execution order:
where
> Aggregation function >having
.- Supports multi-field grouping, the specific syntax is:
group by columnA,columnB
A.Group according to gender, statistics男性员工 和 女性员工的数量
SELECT gender, COUNT(*) FROM empolyee GROUP BY gender;
B.Group according to gender, statistics男性员工 和 女性员工的平均年龄
SELECT gender, AVG(age) FROM empolyee GROUP BY gender;
C. Query 年龄小于45
the employee and 工作地址分组
obtain 员工数量大于等于2
the work address based on
SELECT workaddress, count(*) address_count
FROM empolyee
WHERE age < 45
GROUP BY workaddress
HAVING address_count >= 2;
D. Statistics 工作地址
of male and female employees working数量
SELECT workaddress, gender, count(*) '数量'
FROM empolyee
GROUP BY gender , workaddress;
6 Sorting query
grammar
SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1 , 字段2 排序方式2 ;
sort by
- ASC: Ascending (default)
- DESC: descending order
Notice:
If it is ascending order, you do not need to specify the sorting method ASC.
If it is a multi-field sort, only when the first field has the same value, the second field will be sorted.
Case:
A. Conduct tests on employees based on their age升序排序
SELECT * FROM empolyee ORDER BY age ASC;
SELECT * FROM empolyee ORDER BY age;
B. Conduct training on employees based on their joining date降序排序
SELECT * FROM empolyee ORDER BY entrydate DESC;
C. Treat employees according to their age 升序排序
. If they are the same age, they will be assigned according to their joining date.降序排序
SELECT * FROM empolyee ORDER BY age ASC , entrydate DESC;
7 Paging query
grammar
SELECT 字段列表 FROM 表名 LIMIT 起始索引, 查询记录数 ;
Precautions:
- The starting index starts from 0, starting index = (query page number - 1) * number of records displayed on each page.
- Paging query is a database dialect, and different databases have different implementations. In MySQL, it is LIMIT.
- If the query is for the first page of data, the starting index can be omitted and simply abbreviated to limit 10.
Case
A. Query employee data on page 1 and display 10 records on each page
SELECT * FROM empolyee LIMIT 0, 5;
SELECT * FROM empolyee LIMIT 5;
B. Query the employee data on page 2, display 10 records on each page ------> (page number -1) * number of records displayed on the page
SELECT * FROM empolyee LIMIT 5, 5;
8 cases
Query female employees with ages 20, 21, 22, and 23
SELECT * FROM empolyee
WHERE gender='女'
AND age in (20,21,22,23);
Query employees whose gender is male and whose age is between 22-23 and whose name is 2 characters.
SELECT * FROM empolyee
WHERE gender='男'
AND age
BETWEEN 22 AND 23
AND name LIKE '__';
Query the number of male and female employees who are younger than 60
SELECT gender, COUNT(*) FROM empolyee
WHERE age < 60
GROUP BY gender;
Query the names and ages of employees who are 35 years old or younger, and sort the query results in ascending order of age. If the ages are the same, sort the results in descending order of joining date.
SELECT name, age FROM empolyee
WHERE age <= 35
ORDER BY age ASC, entrydate DESC;
Query the information of 5 employees whose gender is male and whose age is between 20-40. Sort the results in ascending order of age. If the age is the same, sort the results in ascending order of joining date.
SELECT * FROM empolyee
WHERE gender='男' AND
age BETWEEN 20 AND 40
ORDER BY age ASC, entrydate DESC LIMIT 5;