Mysql tutorial (5): DQL learning

Mysql tutorial (5): DQL learning

DQL Data Query Languagedata 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 表名;

ASCan also be omitted:

SELECT 字段1 [ 别名1 ] , 字段2 [ 别名2 ] ... FROM 表名;

Remove duplicate records

SELECT DISTINCT 字段列表 FROM 表名;

Case

A. Query the specified field name, workno, ageand return

select name, worknum, age from empolyee;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-mVs1bEXJ-1689737094170) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719105556960.png)]

B. Check the employee’s ID number and create an alias

SELECT name, idcard '身份证号' FROM empolyee;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-PrhV6Hpc-1689737094171) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719105748800.png)]

C. Query the ages of employees without duplication

SELECT DISTINCT age '年龄' FROM empolyee;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-ELVu3trW-1689737094171) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719105926010.png)]

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 年龄等于21employee information

SELECT * FROM empolyee WHERE age=21;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-XoNBZSEV-1689737094172) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719110525388.png)]

Query 年龄大于21employee information

SELECT * FROM empolyee WHERE age>21;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-KHMgIzSL-1689737094172) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719110552571.png)]

Query 身份证号为空employee information

SELECT * FROM empolyee WHERE idcard IS NULL;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-XRdd492C-1689737094172) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719110959208.png)]

Query 身份证号不为空employee information

SELECT * FROM empolyee WHERE idcard IS NOT NULL;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-91EtIAcj-1689737094172) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719111042476.png)]

Query 年龄不等于21employee information

SELECT * FROM empolyee WHERE age != 21;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-0ZAwHW1g-1689737094173) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719111134473.png)]

Query 年龄大于等于22并且小于等于24employee 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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-qOna1zHU-1689737094173) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719111257433.png)]

Query 年龄为24and 性别为女employee information

SELECT * FROM empolyee WHERE age=24 AND gender='女';

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-Dun3vDo6-1689737094173) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719111532428.png)]

Inquiry 年龄为24or 21employee information

SELECT * FROM empolyee WHERE age=24 OR age=21;

SELECT * FROM empolyee WHERE age IN (21, 24);

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-kmFw4yJB-1689737094173) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719111608847.png)]

Query 姓名为两个字employee information

SELECT * FROM empolyee WHERE name like '__';

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-nCJeV1ni-1689737094173) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719111815549.png)]

Query 身份证号最后一位是Xemployee information

SELECT * FROM empolyee WHERE idcard LIKE '%X';

SELECT * FROM empolyee WHERE idcard LIKE '_________________X';

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-AabY1qWa-1689737094174) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719112039324.png)]

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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-wY6mSGVv-1689752070614) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719153316863.png)]

B. Statistics of employees平均年龄

SELECT AVG(age) FROM empolyee;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-Mgc4q1mo-1689752070614) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719153303255.png)]

C. Statistics employees最大年龄

SELECT MAX(age) FROM empolyee;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-tAxfPFkc-1689752070614) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719153255630.png)]

D. Statistics of employees最小年龄

SELECT MIN(age) FROM empolyee;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-H9CTmqkk-1689752070614) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719153246045.png)]

E. Statistics of male employees年龄之和

SELECT SUM(age) FROM empolyee WHERE gender = '男';

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-CveZdeuh-1689752070615) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719153236042.png)]

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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-79a5Rg20-1689752870980) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719154635811.png)]

B.Group according to gender, statistics男性员工 和 女性员工的平均年龄

SELECT gender, AVG(age) FROM empolyee GROUP BY gender;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-6a1AH9qN-1689752870980) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719154628571.png)]

C. Query 年龄小于45the employee and 工作地址分组obtain 员工数量大于等于2the work address based on

SELECT workaddress, count(*) address_count
FROM empolyee
WHERE age < 45
GROUP BY workaddress
HAVING address_count >= 2;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-xeQtPjgv-1689752870980) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719154621138.png)]

D. Statistics 工作地址of male and female employees working数量

SELECT workaddress, gender, count(*) '数量'
FROM empolyee
GROUP BY gender , workaddress;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-4QEKn9qk-1689752870980) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719154611960.png)]

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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-aoV3w0xl-1689753482314) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719155457723.png)]

B. Conduct training on employees based on their joining date降序排序

SELECT * FROM empolyee ORDER BY entrydate DESC;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-nHn45H0r-1689753482314) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719155601265.png)]

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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-HbPklQzS-1689753482315) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719155710736.png)]

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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-Otz8HjdH-1689753848341) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719160302119.png)]

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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-xRrWDktE-1689753848342) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719160355413.png)]

8 cases

Query female employees with ages 20, 21, 22, and 23

SELECT * FROM empolyee 
WHERE gender='女' 
AND age in (20,21,22,23);

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-VWzMVFje-1689754717639) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719160611781.png)]

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 '__';

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-2pqKwdyQ-1689754717639) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719160655552.png)]

Query the number of male and female employees who are younger than 60

SELECT gender, COUNT(*) FROM empolyee 
						WHERE age < 60 
						GROUP BY gender;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-hnslKXqM-1689754717640) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719161119264.png)]

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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-0bDeHciB-1689754717640) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719161457772.png)]

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;

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-LjrbrLAE-1689754717640) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20230719161757900.png)]

9 Execution sequence

Insert image description here

Guess you like

Origin blog.csdn.net/WwLK123/article/details/131804855