SQL_DQL_ query statement

DQL_ query records in the table

select * from 表名;

  • 1. Syntax :
		select
					字段列表
		from
					表名列表
		where
					条件列表
		group by
					分组字段
		having
					分组之后的条件
		order by
					排序
		limit
					分页限定
  • 2. Basic query

2.1 Query of multiple fields

		select 字段名1,字段名2... from 表名;
* 注意:
			* 如果查询所有字段,则可以使用*来替代字段列表。

2.2 Remove duplication:

		* distinct

2.3. Calculation columns
* Generally, four arithmetic operations can be used to calculate the values ​​of some columns. (Generally only numerical calculations are performed)

		* ifnull(表达式1,表达式2):null参与的运算,计算结果都为null
			* 表达式1:哪个字段需要判断是否为null
			* 如果该字段为null后的替换值。

2.4. Alias:

		* as:as也可以省略

Example:
Create a student table
Insert picture description here
Query name and age:
Insert picture description here
Query addresses, and remove duplicate addresses.
Insert picture description here
Calculate the sum of math and English scores. Create
Insert picture description here
Insert picture description here
aliases
Insert picture description here

  • 3. Condition query

3.1 where clause followed by conditions

3.2 Operators
*>, <, <=, >=, =, <>
* BETWEEN...AND
* IN (collection)
* LIKE: fuzzy query
* placeholder:
* _: single arbitrary character
* %: multiple arbitrary characters
* IS NULL
* and or &&
* or or ||
* not or!

Example:

				-- 查询年龄大于20岁

				SELECT * FROM student WHERE age > 20;
				
				SELECT * FROM student WHERE age >= 20;
				
				-- 查询年龄等于20岁
				SELECT * FROM student WHERE age = 20;
				
				-- 查询年龄不等于20岁
				SELECT * FROM student WHERE age != 20;
				SELECT * FROM student WHERE age <> 20;
				
				-- 查询年龄大于等于20 小于等于30
				
				SELECT * FROM student WHERE age >= 20 &&  age <=30;
				SELECT * FROM student WHERE age >= 20 AND  age <=30;
				SELECT * FROM student WHERE age BETWEEN 20 AND 30;
				
				-- 查询年龄22岁,18岁,25岁的信息
				SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25
				SELECT * FROM student WHERE age IN (22,18,25);
				
				-- 查询英语成绩为null
				SELECT * FROM student WHERE english = NULL; -- 不对的。null值不能使用 = (!=) 判断
				
				SELECT * FROM student WHERE english IS NULL;
				
				-- 查询英语成绩不为null
				SELECT * FROM student WHERE english  IS NOT NULL;
	


				-- 查询姓马的有哪些? like
				SELECT * FROM student WHERE NAME LIKE '马%';
				-- 查询姓名第二个字是化的人
				
				SELECT * FROM student WHERE NAME LIKE "_化%";
				
				-- 查询姓名是3个字的人
				SELECT * FROM student WHERE NAME LIKE '___';
				
				
				-- 查询姓名中包含德的人
				SELECT * FROM student WHERE NAME LIKE '%德%';

Insert picture description here

Supplement: DQL: query statement

  • 1. Sort query

    • Syntax: order by clause
      * order by sort field 1 sort method 1, sort field 2 sort method 2...

    • Sorting method:
      * ASC: Ascending order, the default.
      * DESC: descending order.

    • Note:
      * If there are multiple sorting conditions, the second condition will be judged only when the current condition value is the same.

Insert picture description here

  • 2. Aggregate functions:
  • Take a column of data as a whole and perform vertical calculations.
    1. Count: calculate the number.
    Generally select non-empty columns: primary key
    count(*)
    2. max: calculate the maximum value
    3. min: calculate the minimum value
    4. sum: calculate and
    5. avg: calculate the average
    Insert picture description here

Insert picture description here

* 注意:聚合函数的计算,排除null值。
		解决方案:
			
			1. 选择不包含非空的列进行计算
			2. IFNULL函数
  • 3. Group query:
    1. Syntax: group by group field;
    2. Note:

    	1. 分组之后查询的字段:分组字段、聚合函数
    	2. where 和 having 的区别?
    		1. where 在分组之前进行限定,如果不满足条件,则不参与分组。
    		 having在分组之后进行限定,如果不满足结果,则不会被查询出来
    		2. where 后不可以跟聚合函数,having可以进行聚合函数的判断。
    
-- 按照男女性别分组,分别查询男、女同学的平均分

SELECT sex,AVG(math) FROM student GROUP BY sex;

-- 按照性别分组。分别查询男女同学的平均分,人数

SELECT sex,AVG(math),COUNT(id) FROM student GROUP BY sex;

-- 按照性别分组。分别查询男女同学的平均分,人数。要求:分数低于80分的人。不参与分组

SELECT sex,AVG(math),COUNT(id) FROM student WHERE math > 80 GROUP BY sex;

-- 按照性别分组。分别查询男女同学的平均分,人数。要求:分数低于80分的人。不参与分组,人数大于3

SELECT sex,AVG(math),COUNT(id) FROM student WHERE math > 80 GROUP BY sex HAVING COUNT(id) > 3;


Insert picture description here

  • 4. Paging query
  1. Syntax: index starting with limit, the number of queries per page;
  2. Formula: starting index = (current page number-1) * number of items displayed on each page
			-- 每页显示3条记录 

			SELECT * FROM student LIMIT 0,3; -- 第1页
			
			SELECT * FROM student LIMIT 3,3; -- 第2页
			
			SELECT * FROM student LIMIT 6,3; -- 第3页


3. limit is a MySQL "dialect"
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109269719