『 数据库 』MySQL复习 - MySQL表CRUD操作全解析
文章目录
- 1 表的 CRUD 操作
- 1.1 Create 操作
- 1.1.1 插入否则更新
- 1.1.2 替换插入
- 1.2 Retrieve 操作
- 1.2.1 全列查询
- 1.2.2 指定列查询
- 1.2.3 结果去重
- 1.2.4 WHERE 条件子句
- 1.2.4.1 运算符
- 1.2.4.2 查询案例
- 1.2.5 NULL 查询
- 1.2.6 结果排序
- 1.2.7 查询分页
1 表的 CRUD 操作
即表内数据的增删改查操作;
-
C
操作即
Create
操作, 对表内数据进行插入; -
R
操作即
Retrieve
操作, 对表内数据进行读取(查找); -
U
操作即
Update
操作, 对表内数据进行更新; -
D
操作即
Delete
操作, 对表内数据进行删除;
1.1 Create 操作
对表内数据进行插入, 通常采用INSERT [INTO]
-
语法
INSERT [INTO] table_name [(column [, column] ...)]VALUES (value_list) [, (value_list)] ... ;
-
value_list
value, [, value] ...
value_list
即数据列表;
上文出现的
[]
中括号的内容可根据需求进行省略;INSERT
插入时可以选择全列插入或是指定列插入;-
指定列插入
指定列插入时,
table_name
后可跟一个括号, 括号内指定需要插入的列名,value
后对指定列传入对应的参数;mysql> create table if not exists students1(-> stu_id char(20) primary key,-> stu_name varchar(20),-> stu_age int unsigned,-> stu_sex enum("man", "woman")-> ); Query OK, 0 rows affected (0.03 sec)mysql> insert into students1 -> (stu_id, stu_name) # 指定列为 stu_id与stu_name两列-> values-> ("0000001", "LiuYi"); # 对指定列的数据进行插入 Query OK, 1 row affected (0.00 sec)mysql> select * from students1; +---------+----------+---------+---------+ | stu_id | stu_name | stu_age | stu_sex | +---------+----------+---------+---------+ | 0000001 | LiuYi | NULL | NULL | +---------+----------+---------+---------+ 1 row in set (0.00 sec)
-
全列插入
全列插入时有两种做法;
- 省略
table_name
后的(column [, column] ...)
- 指定列插入时指定所有列
通常使用省略指定列名的方式来进行全列插入;
mysql> select * from students1; +---------+----------+---------+---------+ | stu_id | stu_name | stu_age | stu_sex | +---------+----------+---------+---------+ | 0000001 | LiuYi | NULL | NULL | +---------+----------+---------+---------+ 1 row in set (0.00 sec)mysql> insert into students1 # 通过省略进行全列插入-> values-> ("0000002", "ChenEr", 18, 1); Query OK, 1 row affected (0.01 sec)mysql> insert into students1 # 指定列时选择全列完成全列插入-> (stu_id, stu_name, stu_age, stu_sex)-> values-> ("0000003", "ZhangSan", 21, 2); Query OK, 1 row affected (0.00 sec)mysql> select * from students1; # 对表查询 +---------+----------+---------+---------+ | stu_id | stu_name | stu_age | stu_sex | +---------+----------+---------+---------+ | 0000001 | LiuYi | NULL | NULL | | 0000002 | ChenEr | 18 | man | | 0000003 | ZhangSan | 21 | woman | +---------+----------+---------+---------+ 3 rows in set (0.00 sec)
- 省略
-
对表进行插入时可以进行单行插入与多行插入, 单行插入与多行插入的区别即为values
后跟的(value_list)
数量;
无论是单行插入还是多行插入, 都必须遵守其需要插入的列(指定列或是全列);
mysql> select * from students1;
+---------+----------+---------+---------+
| stu_id | stu_name | stu_age | stu_sex |
+---------+----------+---------+---------+
| 0000001 | LiuYi | NULL | NULL |
| 0000002 | ChenEr | 18 | man |
| 0000003 | ZhangSan | 21 | woman |
+---------+----------+---------+---------+
3 rows in set (0.00 sec)mysql> insert into students1-> (stu_id, stu_name)-> values-> ("0000004", "LiSi"), # 第一行-> ("0000005", "WangWu"), # 第二行-> ("0000006", "ZhaoLiu"); # 第三行
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql> select * from students1;
+---------+----------+---------+---------+
| stu_id | stu_name | stu_age | stu_sex |
+---------+----------+---------+---------+
| 0000001 | LiuYi | NULL | NULL |
| 0000002 | ChenEr | 18 | man |
| 0000003 | ZhangSan | 21 | woman |
| 0000004 | LiSi | NULL | NULL |
| 0000005 | WangWu | NULL | NULL |
| 0000006 | ZhaoLiu | NULL | NULL |
+---------+----------+---------+---------+
6 rows in set (0.00 sec)
1.1.1 插入否则更新
数据插入时若是主键或是唯一键发生冲突时将会插入失败;
# 主键约束插入失败
mysql> insert into students1 (stu_id) values("0000001");
ERROR 1062 (23000): Duplicate entry '0000001' for key 'students1.PRIMARY'# 唯一键约束插入失败
mysql> insert into students1 (stu_id, stu_name) values("0000007", "ZhangSan");
ERROR 1062 (23000): Duplicate entry 'ZhangSan' for key 'students1.stu_name'
当出现插入重复数据时由于主键/唯一键的唯一性导致插入失败的原因可以使用选择性的进行同步更新, 即插入数据, 若是由于键值冲突而插入失败则进行更新;
-
语法
INSERT ... ON DUPLICATE KEY UPDATE column = value [, column = value] ...
-
示例
mysql> insert into students1 (stu_id) values("0000001") on duplicate key update stu_name = "LiuYi_1"; # 对stu_id进行插入, 如果出现冲突则更新该行stu_name的值 Query OK, 2 rows affected (0.00 sec)mysql> select * from students1; +---------+----------+---------+---------+ | stu_id | stu_name | stu_age | stu_sex | +---------+----------+---------+---------+ | 0000001 | LiuYi_1 | NULL | NULL | | 0000002 | ChenEr | 18 | man | | 0000003 | ZhangSan | 21 | woman | | 0000004 | LiSi | NULL | NULL | | 0000005 | WangWu | NULL | NULL | | 0000006 | ZhaoLiu | NULL | NULL | +---------+----------+---------+---------+ 6 rows in set (0.00 sec)
1.1.2 替换插入
冲突替换既可以使用ON DUPLICATE
更新外还可以使用REPLACE
进行替换;
-
REPLACE INTO
语法REPLACE INTO table_name [(column [, column] ...)]VALUES (value_list) [, (value_list)] ... ;
与
INSERT
语法类似;
REPLACE
与ON DUPLICATE
的区别在于:
-
REPLACE INTO
- 主键或唯一键没有冲突则插入
- 主键或唯一键发生冲突则删除后再插入
-
ON DUPLICATE
- 主键或唯一键没有冲突则插入
- 主键或唯一键发生冲突则更新冲突行
mysql> replace into students1 (stu_id, stu_name) values-> ("0000001", "LiuYi"),-> ("00000002", "ChenEr");
Query OK, 4 rows affected (0.01 sec)
Records: 2 Duplicates: 2 Warnings: 0mysql> select * from students1;
+----------+----------+---------+---------+
| stu_id | stu_name | stu_age | stu_sex |
+----------+----------+---------+---------+
| 00000002 | ChenEr | NULL | NULL |
| 0000001 | LiuYi | NULL | NULL |
| 0000003 | ZhangSan | 21 | woman |
| 0000004 | LiSi | NULL | NULL |
| 0000005 | WangWu | NULL | NULL |
| 0000006 | ZhaoLiu | NULL | NULL |
+----------+----------+---------+---------+
6 rows in set (0.00 sec)
1.2 Retrieve 操作
-
例表
-
建表操作
CREATE TABLE student_scores (student_id CHAR(8) PRIMARY KEY COMMENT '学生学号,主键',student_name VARCHAR(20) NOT NULL COMMENT '学生姓名,不能为空',student_gender ENUM('man', 'woman') NOT NULL COMMENT '学生性别,枚举类型:man或woman',student_age INT UNSIGNED COMMENT '学生年龄,无符号整数',subject1_score INT NOT NULL DEFAULT 0 COMMENT '科目一成绩,不能为空,默认值0表示缺考',subject2_score INT NOT NULL DEFAULT 0 COMMENT '科目二成绩,不能为空,默认值0表示缺考',subject3_score INT NOT NULL DEFAULT 0 COMMENT '科目三成绩,不能为空,默认值0表示缺考',total_score INT NOT NULL DEFAULT 0 COMMENT '学生总分,不能为空,默认值0',average_score DECIMAL(5,2) NOT NULL DEFAULT 0.00 COMMENT '学生平均分,不能为空,保留2位小数,默认值0.00' ) COMMENT = '学生成绩表';
-
数据插入
CREATE TABLE student_scores (student_id CHAR(8) PRIMARY KEY COMMENT '学生学号,主键',student_name VARCHAR(20) NOT NULL COMMENT '学生姓名,不能为空',student_gender ENUM('man', 'woman') NOT NULL COMMENT '学生性别,枚举类型:man或woman',student_age INT UNSIGNED COMMENT '学生年龄,无符号整数',subject1_score INT NOT NULL DEFAULT 0 COMMENT '科目一成绩,不能为空,默认值0表示缺考',subject2_score INT NOT NULL DEFAULT 0 COMMENT '科目二成绩,不能为空,默认值0表示缺考',subject3_score INT NOT NULL DEFAULT 0 COMMENT '科目三成绩,不能为空,默认值0表示缺考',total_score INT NOT NULL DEFAULT 0 COMMENT '学生总分,不能为空,默认值0',average_score DECIMAL(5,2) NOT NULL DEFAULT 0.00 COMMENT '学生平均分,不能为空,保留2位小数,默认值0.00' ) COMMENT = '学生成绩表';
-
Retrieve
操作即为对数据进行读取操作, 也可以称为查询操作;
通常使用SELECT ... FROM ...
的方式进行查询;
-
语法
SELECT [DISTINCT] /* 去重 */{* | column [, column] ... } /* 必须选择星号或具体列 */[FROM table_name] /* 指定查询的表 */[WHERE ...] /* 行级条件筛选 */[GROUP BY ...] /* 分组(可选)*/[HAVING ...] /* 分组后的条件筛选 也可单独使用 */[ORDER BY column [ASC | DESC], ...] /* 排序 */[LIMIT ... [OFFSET ...]]; /* 限制返回行数 */
对于这个语法的执行顺序如下:
-
FROM
- 确定数据来源 -
WHERE
- 对数据进行条件筛选 -
SELECT
- 展示SELECT
可以展示多种内容, 表格内容, 函数结果, 或是运算结果;mysql> select 1+1; +-----+ | 1+1 | +-----+ | 2 | +-----+ 1 row in set (0.00 sec)mysql> select "hello MySQL"; +-------------+ | hello MySQL | +-------------+ | hello MySQL | +-------------+ 1 row in set (0.00 sec)mysql> select database(); +------------+ | database() | +------------+ | crud_DB | +------------+ 1 row in set (0.00 sec)
也可以对展示的结果进行重命名, 重命名的方式通常可以使用
AS
或是直接在SELECT
展示的结果后直接跟上需要重命名的名字;mysql> select 1+1 as "result for 1+1"; +----------------+ | result for 1+1 | +----------------+ | 2 | +----------------+ 1 row in set (0.00 sec)mysql> select 1+1 "result for 1+1", 2+2 "result for 2+2"; +----------------+----------------+ | result for 1+1 | result for 2+2 | +----------------+----------------+ | 2 | 4 | +----------------+----------------+ 1 row in set (0.00 sec)
-
ORDER BY
- 对结果排序 -
LIMIT
- 最终结果限制
-
1.2.1 全列查询
-
语法
select * from table_name;
通常不推荐使用
*
进行全列查询, 本质原因是查询的列越多, 意味着需要传输的数据就更大;使用全列查询页可能会影响到索引的使用;
1.2.2 指定列查询
查询时指定列进行查询, 通常为SELECT 1_column_name, 2_column_name ... FROM table_name
;
mysql> select student_id, student_name, student_gender from student_scores;
+------------+--------------+----------------+
| student_id | student_name | student_gender |
+------------+--------------+----------------+
| 20250001 | LiuYi | man |
| 20250002 | ChenEr | woman |
| 20250003 | ZhangSan | man |
| 20250004 | LiSi | woman |
| 20250005 | WangWu | man |
| 20250006 | ZhaoLiu | woman |
| 20250007 | SunQi | man |
| 20250008 | ZhouBa | woman |
| 20250009 | WuJiu | man |
| 20250010 | ZhengShi | woman |
| 20250011 | FengShiyi | man |
| 20250012 | ChenShier | woman |
| 20250013 | ChuShisan | man |
| 20250014 | WeiShisi | woman |
| 20250015 | ShenShiwu | man |
+------------+--------------+----------------+
15 rows in set (0.00 sec)
1.2.3 结果去重
结果去重采用DISTINCT
选项进行;
mysql> select student_age from student_scores;
# 未采用DISTINCT去重
+-------------+
| student_age |
+-------------+
| 20 |
| 19 |
| 21 |
| 20 |
| 22 |
| 19 |
| 20 |
| 21 |
| 20 |
| 19 |
| 21 |
| 20 |
| 22 |
| 19 |
| 20 |
+-------------+
15 rows in set (0.00 sec)mysql> select distinct student_age from student_scores;
# 采用了DISTINCT去重
+-------------+
| student_age |
+-------------+
| 20 |
| 19 |
| 21 |
| 22 |
+-------------+
4 rows in set (0.01 sec)
1.2.4 WHERE 条件子句
WHERE
条件子句可以通过条件来筛选一些数据;
1.2.4.1 运算符
运算符通常分为两种, 即比较运算符和逻辑运算符, 两个运算符都是配合WHERE
条件子句进行使用;
-
比较运算符
比较运算符 说明 >, >=, <, <= 大于,大于等于,小于,小于等于 = 等于,NULL 不安全,例如 NULL = NULL 的结果是 NULL <=> 等于,NULL 安全,例如 NULL <=> NULL 的结果是 TRUE(1) !=, <> 不等于 BETWEEN a0 AND a1 范围匹配,[a0, a1],如果 a0 <= value <= a1,返回 TRUE(1) IN (option, ...) 如果是 option 中的任意一个,返回 TRUE(1) IS NULL 是 NULL IS NOT NULL 不是 NULL LIKE 模糊匹配。% 表示任意多个(包括 0 个)任意字符;_ 表示任意一个字符
-
逻辑运算符
运算符 说明 AND 多个条件必须都为 TRUE(1),结果才是 TRUE(1) OR 任意一个条件为 TRUE(1),结果为 TRUE(1) NOT 条件为 TRUE(1),结果为 FALSE(0)
1.2.4.2 查询案例
以上文中提供的student_scores
表为例;
-
查询出
subject1_score < 80
的student_name
与对应的成绩subject1_score
;- 通过比较运算符
<
或>
对subject1_score
和80
进行比较
mysql> select student_name, subject1_score from student_scores where subject1_score < 80; # where 子句配合比较运算符'<'得出结果 +--------------+----------------+ | student_name | subject1_score | +--------------+----------------+ | ZhangSan | 76 | | WangWu | 65 | | ZhouBa | 0 | | WuJiu | 78 | | ChenShier | 79 | | WeiShisi | 0 | +--------------+----------------+ 6 rows in set (0.00 sec)
- 通过比较运算符
-
查询出
subject2_score >= 80 && subject2_score <= 95
的student_name
与对应的成绩subject2_score
;- 通过
AND
连接两个比较结果
mysql> select student_name, subject2_score from student_scores where subject2_score between 80 and 95; # where 子句配合比较运算符'BETWEEN a0 ADN a1'得出结果 +--------------+----------------+ | student_name | subject2_score | +--------------+----------------+ | LiuYi | 92 | | ChenEr | 88 | | ZhangSan | 85 | | ZhouBa | 86 | | WuJiu | 82 | | ZhengShi | 89 | | ChenShier | 85 | | ChuShisan | 91 | | ShenShiwu | 87 | +--------------+----------------+ 9 rows in set (0.00 sec)
- 通过
-
查询出
subject3_score
等于90
或91
或94
,95
的student_name
与对应的成绩subject3_score
;- 可以采用两种方式
- 采用逻辑运算符
OR
逐个比对 - 采用
IN
的方式判断对应列的数值是否在这一组中(90, 91, 94, 95)
- 采用逻辑运算符
mysql> select student_name, subject3_score from student_scores where subject3_score = 90 or subject3_score = 91 or subject3_score = 94 or subject3_score = 95; # 直接采用'OR'的方式进行逻辑筛选 +--------------+----------------+ | student_name | subject3_score | +--------------+----------------+ | ChenEr | 95 | | LiSi | 91 | | ZhaoLiu | 94 | | ZhouBa | 90 | +--------------+----------------+ 4 rows in set (0.00 sec)mysql> select student_name, subject3_score from student_scores where subject3_score in (90, 91, 94, 95); # 采用'IN'的方式进行筛选 +--------------+----------------+ | student_name | subject3_score | +--------------+----------------+ | ChenEr | 95 | | LiSi | 91 | | ZhaoLiu | 94 | | ZhouBa | 90 | +--------------+----------------+ 4 rows in set (0.00 sec)
- 可以采用两种方式
-
查询表中
student_id
中末尾编号为单数的student_id
(2025000x
)及student_name
中带字符u
的student_id
与student_name
;-
两个条件
-
student_id
中末尾编号为单数的student_id
(2025000x
)采用模糊查询
_
表示模糊查询某位; -
student_name
中带字符u
的采用模糊查询, 采用
%
模糊查询若干位;
-
mysql> select student_id, student_name from student_scores where student_id like "2025000_" or student_name like "%u%"; +------------+--------------+ | student_id | student_name | +------------+--------------+ | 20250001 | LiuYi | | 20250002 | ChenEr | | 20250003 | ZhangSan | | 20250004 | LiSi | | 20250005 | WangWu | | 20250006 | ZhaoLiu | | 20250007 | SunQi | | 20250008 | ZhouBa | | 20250009 | WuJiu | | 20250013 | ChuShisan | | 20250015 | ShenShiwu | +------------+--------------+ 11 rows in set (0.00 sec)
-
-
筛选出
subject3_score > subject1_score
的student_name
与subject3_score
;- 同样使用比较运算符对两列数据进行比较
mysql> select student_name, subject3_score from student_scores where subject3_score > subject1_score; # 通过比较运算符对 subject3_score 与 subject1_score 进行比较 +--------------+----------------+ | student_name | subject3_score | +--------------+----------------+ | ChenEr | 95 | | ZhangSan | 82 | | LiSi | 91 | | WangWu | 68 | | ZhouBa | 90 | | ZhengShi | 93 | | FengShiyi | 88 | | ChenShier | 81 | | ShenShiwu | 96 | +--------------+----------------+ 9 rows in set (0.00 sec)
-
查询出
total < 200
的student_name
与total_score
;- 主要是用来查找多列合并的表达式
mysql> select student_name, total_score from student_scores where total_score < 200; # 直接查询 +--------------+-------------+ | student_name | total_score | +--------------+-------------+ | ZhouBa | 176 | | FengShiyi | 172 | | WeiShisi | 0 | +--------------+-------------+ 3 rows in set (0.00 sec)mysql> select student_name, subject1_score+subject2_score+subject3_score as total from student_scores where subject1_score+subject2_score+subject3_score < 200; # 查询若干列之和 +--------------+-------+ | student_name | total | +--------------+-------+ | ZhouBa | 176 | | FengShiyi | 172 | | WeiShisi | 0 | +--------------+-------+ 3 rows in set (0.00 sec)
这里无法使用以下两种查询方式;
-
select student_name, subject1_score+subject2_score+subject3_score as total from student_scores where total < 200;
WHERE
子句的执行是在SELETC
之前, 因此执行where total
时,SELECT
语句并未对其采用AS
重命名, 因此错误; -
select student_name, total from student_scores where subject1_score+subject2_score+subject3_score as total < 200;
AS
重命名只能在SELECT
中使用, 无法在WHERE
中使用(SELECT
阶段进行展示);
-
查询出
student_name
中没有u
字符且subject1_score > 80
的student_name
与subject1_score
;-
两个条件
-
student_name
中没有u
字符先使用
like
模糊查询, 而后通过NOT
取反 -
subject1_score > 80
-
mysql> select student_name, subject1_score from student_scores where /* 条件一 */ subject1_score > 80 and /* 条件二 对 like 模糊查询进行取反 */ not student_name like "%u%"; +--------------+----------------+ | student_name | subject1_score | +--------------+----------------+ | ChenEr | 92 | | LiSi | 88 | | ZhengShi | 91 | | FengShiyi | 84 | +--------------+----------------+ 4 rows in set (0.00 sec)
-
-
查询出
student_name
中存在u
字符, 否则要求total > 200
(非total_score
, 要求表达式自行计算)且subject2_score > subject3_score
的student_name
,subject2_score
与subject3_score
与total
-
两个
OR
并列条件-
student_name
中存在u
字符 -
要求
total > 200
(非total_score
, 要求表达式自行计算)且subject2_score > subject3_score
有两个
AND
并列条件;-
total > 200
(非total_score
, 要求表达式自行计算) -
subject2_score > subject3_score
-
-
SELECT student_name, subject2_score, subject3_score, subject1_score+subject2_score+subject3_score as total FROM student_scores WHERE student_name LIKE "%u%" OR (subject1_score+subject2_score+subject3_score > 200 AND subject2_score > subject3_score); +--------------+----------------+----------------+-------+ | student_name | subject2_score | subject3_score | total | +--------------+----------------+----------------+-------+ | LiuYi | 92 | 78 | 255 | | ZhangSan | 85 | 82 | 243 | | WangWu | 72 | 68 | 205 | | ZhaoLiu | 96 | 94 | 285 | | SunQi | 75 | 79 | 236 | | ZhouBa | 86 | 90 | 176 | | WuJiu | 82 | 76 | 236 | | ChenShier | 85 | 81 | 245 | | ChuShisan | 91 | 86 | 264 | | ShenShiwu | 87 | 96 | 276 | +--------------+----------------+----------------+-------+ 10 rows in set (0.00 sec)
-
1.2.5 NULL 查询
-
存在一张表
-
建表
CREATE TABLE student_info (student_id CHAR(8) PRIMARY KEY COMMENT '学生学号,主键',student_name VARCHAR(20) NOT NULL COMMENT '学生姓名,不能为空',student_age INT UNSIGNED COMMENT '学生年龄',student_gender ENUM('man', 'woman') COMMENT '学生性别',qq_number VARCHAR(15) UNIQUE COMMENT 'QQ号,唯一约束,允许为空',email VARCHAR(50) UNIQUE COMMENT '邮箱,唯一约束,允许为空' ) COMMENT = '学生基本信息表';
-
数据插入
INSERT INTO student_info (student_id, student_name, student_age, student_gender, qq_number, email) VALUES ('20250001', 'LiuYi', 20, 'man', '123456789', 'liuyi@example.com'), ('20250002', 'ChenEr', 19, 'woman', NULL, 'chener@example.com'), ('20250003', 'ZhangSan', 21, 'man', '987654321', NULL), ('20250004', 'LiSi', 20, 'woman', NULL, NULL), ('20250005', 'WangWu', 22, 'man', '555666777', 'wangwu@example.com'), ('20250006', 'ZhaoLiu', 19, 'woman', '888999000', 'zhaoliu@example.com'), ('20250007', 'SunQi', 20, 'man', NULL, 'sunqi@example.com'), ('20250008', 'ZhouBa', 21, 'woman', '111222333', NULL), ('20250009', 'WuJiu', 20, 'man', NULL, NULL), ('20250010', 'ZhengShi', 19, 'woman', '444555666', 'zhengshi@example.com');
-
-
查询出
qq_number
为NULL
或者email
为NULL
的学生的student_id
,student_name
,qq_number
与email
mysql> SELECT student_id, student_name, qq_number, email -> FROM student_info -> WHERE qq_number IS NULL OR email IS NULL; +------------+--------------+-----------+--------------------+ | student_id | student_name | qq_number | email | +------------+--------------+-----------+--------------------+ | 20250002 | ChenEr | NULL | chener@example.com | | 20250003 | ZhangSan | 987654321 | NULL | | 20250004 | LiSi | NULL | NULL | | 20250007 | SunQi | NULL | sunqi@example.com | | 20250008 | ZhouBa | 111222333 | NULL | | 20250009 | WuJiu | NULL | NULL | +------------+--------------+-----------+--------------------+ 6 rows in set (0.00 sec)
-
查询出
qq_number
不为NULL
且email
不为NULL
, 否则要求student_age
大于20且student_gender
为man
的学生的student_id
,student_name
,qq_number
,email
,student_age
与student_gender
-
两个
OR
并列条件-
qq_number
不为NULL
且email
不为NULL
有两个
AND
并列条件;-
qq_number
不为NULL
-
email
不为NULL
-
-
要求
student_age
大于20且student_gender
为’man’有两个
AND
并列条件;-
student_age
大于20 -
student_gender
为’man’
-
-
mysql> SELECT student_id, student_name, qq_number, email, student_age, student_gender-> FROM student_info -> WHERE (qq_number IS NOT NULL AND email IS NOT NULL) -> OR (student_age > 20 AND student_gender = 'man'); +------------+--------------+-----------+----------------------+-------------+----------------+ | student_id | student_name | qq_number | email | student_age | student_gender | +------------+--------------+-----------+----------------------+-------------+----------------+ | 20250001 | LiuYi | 123456789 | liuyi@example.com | 20 | man | | 20250003 | ZhangSan | 987654321 | NULL | 21 | man | | 20250005 | WangWu | 555666777 | wangwu@example.com | 22 | man | | 20250006 | ZhaoLiu | 888999000 | zhaoliu@example.com | 19 | woman | | 20250010 | ZhengShi | 444555666 | zhengshi@example.com | 19 | woman | +------------+--------------+-----------+----------------------+-------------+----------------+ 5 rows in set (0.00 sec)
-
1.2.6 结果排序
结果排序的语法通常为ORDER BY column [ ASC | DESC ]
;
SELECT ... FROM table_name [WHERE ...] ORDER BY column [ ASC | DESC ], [...];
-
ASC
升序, 结果从小到大进行排列;
-
DESC
降序, 结果从大到小进行排列;
默认情况下为ASC
;
通常情况下, 该语句的执行顺序是比较靠后的, 本质上需要等待[FROM]
, [WHERE]
, JOIN
等子句筛选出所有符合条件的记录, 记录筛选后通过ORDER BY
语句对结果进行排序, 最后通过LIMIT
语句进行分页;
|
mysql> select * from student_scores;
+------------+--------------+----------------+-------------+----------------+----------------+----------------+-------------+---------------+
| student_id | student_name | student_gender | student_age | subject1_score | subject2_score | subject3_score | total_score | average_score |
+------------+--------------+----------------+-------------+----------------+----------------+----------------+-------------+---------------+
| 20250001 | LiuYi | man | 20 | 85 | 92 | 78 | 255 | 85.00 |
| 20250002 | ChenEr | woman | 19 | 92 | 88 | 95 | 275 | 91.67 |
| 20250003 | ZhangSan | man | 21 | 76 | 85 | 82 | 243 | 81.00 |
| 20250004 | LiSi | woman | 20 | 88 | 79 | 91 | 258 | 86.00 |
| 20250005 | WangWu | man | 22 | 65 | 72 | 68 | 205 | 68.33 |
| 20250006 | ZhaoLiu | woman | 19 | 95 | 96 | 94 | 285 | 95.00 |
| 20250007 | SunQi | man | 20 | 82 | 75 | 79 | 236 | 78.67 |
| 20250008 | ZhouBa | woman | 21 | 0 | 86 | 90 | 176 | 58.67 |
| 20250009 | WuJiu | man | 20 | 78 | 82 | 76 | 236 | 78.67 |
| 20250010 | ZhengShi | woman | 19 | 91 | 89 | 93 | 273 | 91.00 |
| 20250011 | FengShiyi | man | 21 | 84 | 0 | 88 | 172 | 57.33 |
| 20250012 | ChenShier | woman | 20 | 79 | 85 | 81 | 245 | 81.67 |
| 20250013 | ChuShisan | man | 22 | 87 | 91 | 86 | 264 | 88.00 |
| 20250014 | WeiShisi | woman | 19 | 0 | 0 | 0 | 0 | 0.00 |
| 20250015 | ShenShiwu | man | 20 | 93 | 87 | 96 | 276 | 92.00 |
+------------+--------------+----------------+-------------+----------------+----------------+----------------+-------------+---------------+
15 rows in set (0.00 sec)
-
查询出
student_id
和subject2_score
, 按subject2_score
升序显示;mysql> select student_name, subject2_score from student_scores order by subject2_score asc; +--------------+----------------+ | student_name | subject2_score | +--------------+----------------+ | FengShiyi | 0 | | WeiShisi | 0 | | WangWu | 72 | | SunQi | 75 | | LiSi | 79 | | WuJiu | 82 | | ZhangSan | 85 | | ChenShier | 85 | | ZhouBa | 86 | | ShenShiwu | 87 | | ChenEr | 88 | | ZhengShi | 89 | | ChuShisan | 91 | | LiuYi | 92 | | ZhaoLiu | 96 | +--------------+----------------+ 15 rows in set (0.00 sec)
-
查询
student_name
,subject1_score
,subject2_score
,subject3_score
, 按subject1_score
降序,subject2_score
升序,subject3_score
升序的方式进行显示;mysql> select student_name, subject1_score, subject2_score, subject3_score from student_scores order by subject1_score desc, subject2_score asc, subject3_score asc; +--------------+----------------+----------------+----------------+ | student_name | subject1_score | subject2_score | subject3_score | +--------------+----------------+----------------+----------------+ | ZhaoLiu | 95 | 96 | 94 | | ShenShiwu | 93 | 87 | 96 | | ChenEr | 92 | 88 | 95 | | ZhengShi | 91 | 89 | 93 | | LiSi | 88 | 79 | 91 | | ChuShisan | 87 | 91 | 86 | | LiuYi | 85 | 92 | 78 | | FengShiyi | 84 | 0 | 88 | | SunQi | 82 | 75 | 79 | | ChenShier | 79 | 85 | 81 | | WuJiu | 78 | 82 | 76 | | ZhangSan | 76 | 85 | 82 | | WangWu | 65 | 72 | 68 | | WeiShisi | 0 | 0 | 0 | | ZhouBa | 0 | 86 | 90 | +--------------+----------------+----------------+----------------+ 15 rows in set (0.00 sec)
这里三个排序条件分别为:
-
首要排序规则
subject1_score
降序; -
次要排序规则
subject2_score
升序; -
第三排序规则
subject3_score
升序;
-
-
查询三个
subject_score
的和并由高到低排序;mysql> select student_name, subject1_score+subject2_score+subject3_score as total from student_scores order by total desc; +--------------+-------+ | student_name | total | +--------------+-------+ | ZhaoLiu | 285 | | ShenShiwu | 276 | | ChenEr | 275 | | ZhengShi | 273 | | ChuShisan | 264 | | LiSi | 258 | | LiuYi | 255 | | ChenShier | 245 | | ZhangSan | 243 | | SunQi | 236 | | WuJiu | 236 | | WangWu | 205 | | ZhouBa | 176 | | FengShiyi | 172 | | WeiShisi | 0 | +--------------+-------+ 15 rows in set (0.00 sec)
在此处的查询语句中, 可以看到结尾的
ORDER BY
使用的是别名, 而在此之前使用WHERE
却无法使用别名, 本质上是因为查询语句的执行顺序;通常情况下,
ORDER BY
语句是在较为后面才执行的, 而WHERE
语句还需要对数据进行筛选,SELECT
语句则是展示部分, 在上述语句中, 执行SELECT
时表示所有数据已经处理完毕了, 可以对需要展示的数据进行SELECT
选择, 选择过后将数据进行ORDER BY
进行排序, 由于别名的生成是在SELECT
中进行的, 因此SELECT
执行后表示这个别名已经存在了, 可以被其他执行优先级较低的其他语句利用;这条语句的执行顺序为:
-
FROM student_scores
- 选择表 -
SELECT student_name, subject1_score+subject2_score+subject3_score AS total
- 选择需要展示的数据与对需要别名的数据进行别名创建 -
ORDER BY total DESC
- 对数据进行排列处理
-
-
查询
student_name
中含u
字符或者e
字符的student_name
和subject1_score
, 按照subject1_score
降序的方式显示mysql> select student_name, subject1_score from student_scores where student_name like '%e%' or student_name like '%u%' order by subject1_score desc; +--------------+----------------+ | student_name | subject1_score | +--------------+----------------+ | ZhaoLiu | 95 | | ShenShiwu | 93 | | ChenEr | 92 | | ZhengShi | 91 | | ChuShisan | 87 | | LiuYi | 85 | | FengShiyi | 84 | | SunQi | 82 | | ChenShier | 79 | | WuJiu | 78 | | WangWu | 65 | | ZhouBa | 0 | | WeiShisi | 0 | +--------------+----------------+ 13 rows in set (0.00 sec)
1.2.7 查询分页
查询分页所使用的查询语句为LIMIT ... OFFSET
;
通常情况下这是所执行的最后一条语句, 对选择, 过滤, 排序后的记录进行分页;
其中LIMIT
表示分页的数量, 如:
LIMIT 3
显示前三条, OFFSET
表示偏移量, 表示偏移多少行, 通常情况(省略时)默认为OFFSET 0
表示不进行偏移;
mysql> select student_id, student_name from student_scores;
+------------+--------------+
| student_id | student_name |
+------------+--------------+
| 20250001 | LiuYi |
| 20250002 | ChenEr |
| 20250003 | ZhangSan |
| 20250004 | LiSi |
| 20250005 | WangWu |
| 20250006 | ZhaoLiu |
| 20250007 | SunQi |
| 20250008 | ZhouBa |
| 20250009 | WuJiu |
| 20250010 | ZhengShi |
| 20250011 | FengShiyi |
| 20250012 | ChenShier |
| 20250013 | ChuShisan |
| 20250014 | WeiShisi |
| 20250015 | ShenShiwu |
+------------+--------------+
15 rows in set (0.00 sec)mysql> select student_id, student_name from student_scores limit 3;
+------------+--------------+
| student_id | student_name |
+------------+--------------+
| 20250001 | LiuYi |
| 20250002 | ChenEr |
| 20250003 | ZhangSan |
+------------+--------------+
3 rows in set (0.00 sec)mysql> select student_id, student_name from student_scores limit 3 offset 2;
+------------+--------------+
| student_id | student_name |
+------------+--------------+
| 20250003 | ZhangSan |
| 20250004 | LiSi |
| 20250005 | WangWu |
+------------+--------------+
3 rows in set (0.00 sec)
以该段语句与结果为例;
-
select student_id, student_name from student_scores;
表示不进行分页, 查询出
student_id
,student_name
信息; -
select student_id, student_name from student_scores limit 3;
查询出
student_id
,student_name
信息并只展示前三条; -
select student_id, student_name from student_scores limit 3 offset 2;
查询出
student_id
,student_name
信息并略过前两条, 从第三条开始展示往后的三条;