Mysql operation DQL

DQL, short for Data Query Language, is a programming language for retrieving data from a database. DQL is a subset of SQL (Structured Query Language), used to query relational databases, such as MySQL, Oracle, and SQL Server.

DQL provides a variety of query operations, such as SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, etc. Using these operations, the desired data can be retrieved based on specific criteria, sorted and grouped in a specific order.

DQL also supports multi-table queries and subqueries, which can jointly retrieve data from multiple tables, and use nested query statements in subqueries for retrieval.

basic query syntax

select ... from ...

select [distinct] ... from ... [where ...] [group by ...] [having ...] [order by ...] [limit ...]

Execution order of query statements

  1. Execute the from clause first: query based on the table
  2. Execute the where clause again: perform conditional filtering or conditional filtering
  3. Then execute the group by clause: perform group query on the remaining data.
  4. Execute the having clause again: after grouping, filter or filter again
  5. Then execute the select clause: the purpose is to select the fields required by the business for display
  6. Then execute the order by clause: sort the selected fields
  7. Finally, execute the limit clause: perform pagination query, or query the first n records

prepare data

Before learning the syntax of the next query, we prepare several tables in advance and insert some data into this table to facilitate our subsequent query operations.

student table

Field Name Field Type illustrate
sid char(6) student number
takes off varchar(50) student name
age int student age
gender varchar(50) student gender
CREATE TABLE stu (
    sid	CHAR(6),
    sname		VARCHAR(50),
    age		INT,
    gender	VARCHAR(50)
);
INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');
INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');
INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');
INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');
INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');
INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');
INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');
INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');
INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');
INSERT INTO stu VALUES('S_1010', 'zhengShi', 5, 'female');
INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);

emp table

Field Name Field Type illustrate
empno int employee ID
name varchar(50) employee's name
job varchar(50) employee work
mgr int leader number
hiredate date Entry date
sal decimal(7,2) monthly salary
comm decimal(7,2) bonus
deptno int part number
CREATE TABLE emp(
    empno	INT,
    ename	VARCHAR(50),
    job		VARCHAR(50),
    mgr		INT,
    hiredate	DATE,
    sal		DECIMAL(7,2),
    comm	decimal(7,2),
    deptno	INT
);
INSERT INTO emp values(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp values(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp values(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp values(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp values(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp values(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp values(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp values(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp values(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp values(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO emp values(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp values(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp values(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp values(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);

dept table

Field Name Field Type illustrate
deptno int partial encoding
dname varchar(50) part name
loc varchar(50) Some locations
CREATE TABLE dept(
    deptno		INT,
    dname		varchar(14),
    loc			varchar(13)
);
INSERT INTO dept values(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept values(20, 'RESEARCH', 'DALLAS');
INSERT INTO dept values(30, 'SALES', 'CHICAGO');
INSERT INTO dept values(40, 'OPERATIONS', 'BOSTON');

basic query

  1. query all columns

    SELECT * FROM stu;
    
  2. Query the specified column

    SELECT sid, sname, age FROM stu;
    

conditional query

Conditional query is to give the WHERE clause when querying. The following operators and keywords can be used in the WHERE clause:

=, !=, <>, <, <=, >, >=, BETWEEN…AND, IN(set), IS NULL, AND, OR, NOT, XOR (exclusive OR)

  1. Query records whose gender is female and whose age is less than 50

    SELECT * FROM stu
    WHERE gender='female' AND  age<50;
    
  2. Query the record with the student number S_1001 or the name liSi

    SELECT * FROM stu WHERE sid ='S_1001' OR sname='liSi';
    
  3. Query records with student numbers S_1001, S_1002, and S_1003

    SELECT * FROM stu WHERE sid IN ('S_1001','S_1002','S_1003');
    

fuzzy query

Query according to fuzzy conditions, you can use LIKE conditions, or REGEXP.

like

like is used after the where clause to indicate a partial match. After like, there are usually two kinds of wildcards:

_ => means match any one character.

% => means match any character.

# 查询所有的姓名以s开头的学生
select * from student where sname like 's%'
# 查询所有的姓名以s开头的,且长度为5的学生
select * from student where sname like 's____'

regexp

Use regular expressions to match strings.

model describe
^ Matches the beginning of the input string. ^ also matches the position after '\n' or '\r' if the Multiline property of the RegExp object is set.
$ Matches the end of the input string. If the Multiline property of the RegExp object is set, $ also matches the position before '\n' or '\r'.
. Matches any single character except "\n".
[...] collection of characters. Matches any one of the contained characters. For example, '[abc]' would match 'a' in "plain".
[^...] Negative character set. Matches any character not contained. For example, '[^abc]' would match the 'p' in "plain".
\d [0-9], matches all digits.
p1|p2|p3 Matches p1 or p2 or p3. For example, 'z|food' would match "z" or "food". '(z|f)ood' matches "zood" or "food".
* Matches the preceding subexpression zero or more times. For example, zo* would match "z" as well as "zoo". * is equivalent to {0,}.
+ Matches the preceding subexpression one or more times. For example, 'zo+' would match "zo" and "zoo", but not "z". + is equivalent to {1,}.
{n} is a non-negative integer. Matches exactly n times. For example, 'o{2}' would not match the 'o' in "Bob", but would match both o's in "food".
{n,m} Both m and n are non-negative integers, where n <= m. Matches at least n times and at most m times.
# 查询名字以l开头,以i结尾的
select * from stu  where name regexp '^l|i$'

SELECT 'hello' REGEXP '^he'      结果:1  表示匹配
SELECT 'hello' REGEXP '^hh'      结果:0  表示不匹配

field control

Remove duplicate records

Remove duplicate records (the data on the series in two or more rows of records are the same), for example, there are identical records in the sal field in the emp table. When only the sal field of the emp table is queried, there will be duplicate records. If you want to remove duplicate records, you need to use DISTINCT:

SELECT DISTINCT sal FROM emp;

Calculations Between Columns

View the sum of the employee's monthly salary and commission. Since the sal and comm columns are both numeric types, they can be added. If there is a field in sal or comm that is not a numeric type, an error will occur.

SELECT *,sal+comm FROM emp;

The value of many records in the comm column is NULL, because the result of adding anything to NULL is still NULL, so the settlement result may appear NULL. The function IFNULL that converts NULL to the value 0 is used below

SELECT *,sal+IFNULL(comm,0) FROM emp;

Add an alias to the column name

In the above query, the column name is sal+IFNULL(comm,0), which is very ugly. Now we give this column an ​​alias, which is total:

SELECT *, sal+IFNULL(comm,0) AS total FROM emp;

When aliasing a column, the AS keyword can be omitted:

SELECT *,sal+IFNULL(comm,0)  total FROM emp;

Sort results

  1. Query all student records, sorted by age in ascending order

    SELECT *  FROM stu ORDER BY sage ASC;
    # 或者
    SELECT *  FROM stu ORDER BY sage;
    
  2. Query all student records, sorted by age in descending order

    SELECT *  FROM stu  ORDER BY age DESC;
    
  3. Query all employees, sort by monthly salary in descending order, if the monthly salary is the same, sort by number in ascending order

    SELECT * FROM emp  ORDER BY sal DESC,empno ASC;
    

aggregate function

An aggregate function is a function that acts on a column of data and operates on a column of data. Contains: max, min, sum, count, avg and other common functions.

  • max(): Calculate the maximum value of the specified column data
  • min(): Calculate the minimum value of the specified column data
  • count(): Calculate the number of data in the specified column that is not NULL
  • sum(): Calculate the sum of the values ​​of the specified column, if the type of the calculated column is not a numeric type, the calculation result is 0
  • avg(): Calculate the average value of the values ​​of the specified column, if the type of the calculated column is not a numeric type, the calculated result is 0

The method of use is as follows:

max

-- 用来计算指定列的最大值
-- 计算最高的工资
select max(sal) from emp;

min

-- 用来计算指定列的最小值
-- 计算最低的工资
select min(sal) from emp;

count

-- 用来统计指定列的数据的数量,注意,NULL不会被统计
-- 1. 计算emp表中有多少人有工资sal
select count(sal) from emp;
-- 2. 计算emp表中有多少行数据
select count(*) from emp;

-- count(*) : 用来统计行记录,只要有这一行就会统计,即便这一行的所有的字段值都是NULL,依然算是一个有效的行

sum

-- 用来统计指定列的数据的和,注意,NULL不会被统计
-- 计算emp表中的工资的和
select sum(sal) from emp;

avg

-- 用来统计指定列的数据的平均值,注意,NULL不会被统计
-- 计算emp表中的平均工资
select avg(sal) from emp;

Notice:

In the above requirements, we need to count the average salary of employees. However, in some rows of data, the value corresponding to salary (sal) is NULL.

For example: There are 20 rows of data in the table, and 2 rows of data are NULL. Then when the average is calculated, everyone's salary will be added together, and this sum will be divided by 18 instead of 20. Because aggregate functions do not count NULL values.

If the requirement needs to distribute this sum to everyone, including NULL rows, then this SQL statement needs to be modified:

select avg(ifnull(sal, 0)) from emp;

Group query

When querying, you can group by one or more fields. Rows with the same group field value are considered a group. In general, the meaning of grouping is to perform aggregated statistics on the data of each group, such as counting the number and maximum value of each group.

Note: Only grouping fields and aggregation functions can be included in the query field

group by

-- 查询每一个部门的编号以及这个部门的最高工资(sal)
select deptno, max(sal) from emp group by deptno;

-- 查询每一个工作的名字以及这个工作的人数
select job, count(*) from emp group by job;

-- 查询每一个部门、每一个工作的人数
select deptno, job, count(*) from emp group by deptno, job;

having

having is a control condition for data filtering, similar to where, but different from where:

  1. Having is applied to the data after grouping, and where is applied to the data before grouping. The data filtered out by where does not participate in the grouping.

    Writing method reflects: having needs to be written after group by, where needs to be written before group by.

  2. Aggregate functions can be used after having, but aggregate functions cannot be used where.

-- 查询平均工资高于3000的部门编号及平均工资
select deptno, avg(sal) from emp group by deptno having avg(sal) > 3000;

imitate

The select query statement will query all the data that meets the conditions in a table. The limit keyword can limit the number of rows in the query result.

-- 查询emp表中的第0行开始,5行的数据。
select * from emp limit 0, 5;

-- 查询emp表中从第10行开始,7行的数据。
select * from emp limit 10, 7;

Flexible use of limit can achieve the effect of pagination query.

-- 例如: 我需要在一个页面上显示数据库中的数据,但是页面的大小有限,每一页我需要显示20条数据。
-- 第一页的数据:
select * from news limit 0, 20;
-- 第二页的数据:
select * from news limit 20, 20;
-- 第三页的数据:
select * from news limit 40, 20;

-- 往后的每一个分页的内容,只需要控制好每一次limit的起点即可。

query summary

Query statement writing order

select – from - where - group by - having - order by - limit

Query statement execution order

from - where -group by - having - select - order by-limit

[Qianfeng Education] A full set of tutorials on big data development, the most comprehensive big data learning video in history

Guess you like

Origin blog.csdn.net/longz_org_cn/article/details/131931313