Usage of DQL statement (MySQL)


Preface

This article mainly introduces the functions and usage of DQL statements in SQL statements, including the syntax of DQL statements, basics, conditions, grouping queries and other methods, as well as the usage of some aggregate functions.
Ps: In the introduction of this article, the database management client software Datagrip will be used for operation. It doesn’t matter if you don’t know how to use it. This software is only used for the convenience of display.


1. DQL statement indirection and syntax

1. Introduction to DQL

DQL是一种数据查询语言,用来查询数据库中表的记录。在一个正常的业务系统中,查询操作的频次是要远高于增删查改。

2. DQL syntax

select
	字段列表
from
	表名列表
where
	条件列表
group by
	分组字段列表
having
	分组后条件列表
order by
	排序字段列表
limit
	分页参数

When introducing this article, the complete syntax above will be divided into the following parts for introduction:

  • Basic query (without any conditions)
  • Conditional query (where)
  • Aggregation functions (count, min, max…)
  • Group query (group by)
  • Sorting query (order by)
  • Paging query (limit)

2. Use of DQL statements

Before introducing, we need to generate a table as shown below:
Insert image description here

The sql statement to create this table is as follows. You can use it to create a table that is the same as the blogger's to facilitate practical operation.

# 创建一个名为test的数据库
create schema test;
#在test数据库下创建一个名为
use test
#创建一个名为emp的表
create table emp(
    id int comment '编号',
    workno varchar(10) comment '工号',
    name varchar(10) comment '姓名',
    gender char(1) comment '性别',
    age tinyint unsigned comment '年龄',
    idcard char(18) comment '身份证号',
    workaddress varchar(50) comment '工作地址',
    entrydate date comment '入职时间'
)comment '员工表';

#向表中插入一些数据
INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (1, '00001', '柳岩666', '女', 20, '123456789012345678', '北京', '2000-01-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (2, '00002', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (3, '00003', '韦一笑', '男', 38, '123456789712345670', '上海', '2005-08-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (4, '00004', '赵敏', '女', 18, '123456757123845670', '北京', '2009-12-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (5, '00005', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (6, '00006', '杨逍', '男', 28, '12345678931234567X', '北京', '2006-01-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (7, '00007', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (8, '00008', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (9, '00009', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (10, '00010', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (11, '00011', '张士诚', '男', 55, '123567897123465670', '江苏', '2015-05-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (12, '00012', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (13, '00013', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (14, '00014', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (15, '00015', '胡青牛', '男', 70, '12345674971234567X', '西安', '2018-04-01');

INSERT INTO emp (id, workno, name, gender, age, idcard, workaddress, entrydate)
VALUES (16, '00016', '周芷若', '女', 18, null, '北京', '2012-06-01');

1. Basic query

(1) Query multiple fields

select 字段1,字段2,字段3... from 表名;

For example, we query the specified fields name, workno, age and return:

Insert image description here
If we want to query all the information of the table, we can also use the following code:

select id, workno, name, gender, age, idcard, workaddress, entrydate from emp;

In addition, * can be used to replace all field names (where*No. represents querying all fields):

select * from emp;

(2) Set an alias for the field

select 字段1 [as 别名1],字段2 [as 别名2]... from 表名;

For example, we query the fields name, workno, and age again and alias them as name, work number, and age:

select name as 姓名,workno as 工号,age as 年龄 from emp;

It can be found that the name displayed in this field is different from the English name in the above picture, and its alias is displayed.
Insert image description here

(3) Remove duplicate records

select distinct 字段列表 from 表名;

For example, if we want to query the work addresses of all employees, but do not repeat them:

select distinct workaddress from emp;

Insert image description here
The following article is still being updated...


Summarize

This article mainly introduces how to use DQL statements. I hope it will be helpful to you.

Guess you like

Origin blog.csdn.net/weixin_63614711/article/details/132510941