【MySQL】7. MySQL表数据的增删查改(CRUD)
目录
CRUD 即:Create(创建)、Retrieve(读取)、Update(更新)、Delete(删除)
1. Create(创建 / 插入)
1.1 基础语法
INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value [, value] ...
1.2 案例:学生表操作
-- 创建学生表
create table students(
id int unsigned primary key auto_increment,
sn int unsigned unique key comment '学号',
name varchar(20) not null,
qq vatchar(32) unique key
);
1. 单行数据 + 全列插入
插入时 value_list 的数量和顺序必须与表定义的列完全一致。若字段有自增或默认值,也可省略对应列,让 MySQL 自动填充。
-- 指定列插入
insert into students (sn, name, qq) values (123,'aaa','4567890');
mysql> select * from students;
+----+------+------+---------+
| id | sn | name | qq |
+----+------+------+---------+
| 1 | 123 | aaa | 4567890 |
+----+------+------+---------+
1 row in set (0.00 sec)
-- 全列插入
insert into students values (100,124,'bbb','4567891');
mysql> select * from students;
+-----+------+------+---------+
| id | sn | name | qq |
+-----+------+------+---------+
| 1 | 123 | aaa | 4567890 |
| 100 | 124 | bbb | 4567891 |
+-----+------+------+---------+
2 rows in set (0.00 sec)
-- into也可以省略
insert students values (101,125,'ccc','4567892');
2. 多行数据 + 指定列插入
可显式指定要插入的列,value_list 只需与指定列的数量和顺序一致。
-- 多行指定列插入
insert into students (id, sn, name) values
(1001, 201,'曹操'),
(1002, 202,'许褚');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
-- 查看结果
mysql> select * from students;
+------+------+--------+---------+
| id | sn | name | qq |
+------+------+--------+---------+
| 1 | 123 | aaa | 4567890 |
| 100 | 124 | bbb | 4567891 |
| 101 | 125 | ccc | 4567892 |
| 1001 | 201 | 曹操 | NULL |
| 1002 | 202 | 许褚 | NULL |
+------+------+--------+---------+
5 rows in set (0.00 sec)
-- 多行全列插入
insert into students values
(10001, 301,'刘备','1234560'),
(10002, 302,'诸葛亮','1234561');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
-- 查看结果
mysql> select * from students;
+-------+------+-----------+---------+
| id | sn | name | qq |
+-------+------+-----------+---------+
| 1 | 123 | aaa | 4567890 |
| 100 | 124 | bbb | 4567891 |
| 101 | 125 | ccc | 4567892 |
| 1001 | 201 | 曹操 | NULL |
| 1002 | 202 | 许褚 | NULL |
| 10001 | 301 | 刘备 | 1234560 |
| 10002 | 302 | 诸葛亮 | 1234561 |
+-------+------+-----------+---------+
7 rows in set (0.00 sec)
3. 插入否则更新(ON DUPLICATE KEY UPDATE)
当主键或唯一键的值已存在时,普通插入会因冲突失败:
-- 主键冲突
mysql> insert into students (id, sn, name) values (1001, 2002,'曹操');
ERROR 1062 (23000): Duplicate entry '1001' for key 'students.PRIMARY'
-- 唯一键冲突
mysql> insert into students (id, sn, name) values (2001, 201,'曹操');
ERROR 1062 (23000): Duplicate entry '201' for key 'students.sn'
可使用 ON DUPLICATE KEY UPDATE 在冲突时选择性更新:
insert into students values (1001,2001,'caocao','2345670') on duplicate key update sn=2001, name='caocao', qq='2345670';
-- Query OK, 2 rows affected (0.00 sec)
mysql> select * from students;
+-------+------+-----------+---------+
| id | sn | name | qq |
+-------+------+-----------+---------+
| 1 | 123 | aaa | 4567890 |
| 100 | 124 | bbb | 4567891 |
| 101 | 125 | ccc | 4567892 |
| 1001 | 2001 | caocao | 2345670 |
| 1002 | 202 | 许褚 | NULL |
| 10001 | 301 | 刘备 | 1234560 |
| 10002 | 302 | 诸葛亮 | 1234561 |
+-------+------+-----------+---------+
7 rows in set (0.00 sec)
0 row affected:表中有冲突数据,但冲突值与更新值相同1 row affected:表中无冲突数据,数据被插入2 row affected:表中有冲突数据,且数据已被更新
可通过 ROW_COUNT() 函数获取受影响的行数:
SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)
4. 替换(REPLACE)
- 主键或唯一键无冲突:直接插入
- 主键或唯一键有冲突:先删除旧行,再插入新行
-- 无冲突
mysql> replace into students (sn,name,qq) values (105,'许攸','9876540');
-- Query OK, 2 rows affected (0.00 sec)
mysql> select * from students;
+-------+------+-----------+---------+
| id | sn | name | qq |
+-------+------+-----------+---------+
| 1 | 123 | aaa | 4567890 |
| 100 | 124 | bbb | 4567891 |
| 101 | 125 | ccc | 4567892 |
| 1001 | 2001 | caocao | 2345670 |
| 1002 | 202 | 许褚 | NULL |
| 10001 | 301 | 刘备 | 1234560 |
| 10002 | 302 | 诸葛亮 | 1234561 |
| 10003 | 105 | 许攸 | 9876540 |
+-------+------+-----------+---------+
8 rows in set (0.00 sec)
-- 有冲突 -- 根据id看确实是删除后重新插入的数据
mysql> replace into students (sn,name,qq) values (105,'许攸1','9876540');
Query OK, 2 rows affected (0.00 sec)
mysql> select * from students;
+-------+------+-----------+---------+
| id | sn | name | qq |
+-------+------+-----------+---------+
| 1 | 123 | aaa | 4567890 |
| 100 | 124 | bbb | 4567891 |
| 101 | 125 | ccc | 4567892 |
| 1001 | 2001 | caocao | 2345670 |
| 1002 | 202 | 许褚 | NULL |
| 10001 | 301 | 刘备 | 1234560 |
| 10002 | 302 | 诸葛亮 | 1234561 |
| 10004 | 105 | 许攸1 | 9876540 |
+-------+------+-----------+---------+
8 rows in set (0.00 sec)
1 row affected:表中无冲突数据,数据被插入2 row affected:表中有冲突数据,删除后重新插入
2. Retrieve(读取 / 查询)
2.1 基础语法
SELECT
[DISTINCT] {* | {column [, column] ...}}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
[LIMIT ...]
2.2 案例:成绩表操作
-- 创建成绩表
CREATE TABLE exam_result (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL COMMENT '同学姓名',
chinese float DEFAULT 0.0 COMMENT '语文成绩',
math float DEFAULT 0.0 COMMENT '数学成绩',
english float DEFAULT 0.0 COMMENT '英语成绩'
);
-- 插入测试数据
INSERT INTO exam_result (name, chinese, math, english) VALUES
('唐三藏', 67, 98, 56),
('孙悟空', 87, 78, 77),
('猪悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55, 85, 45),
('孙仲谋', 70, 73, 78),
('宋公明', 75, 65, 30);
-- Query OK, 7 rows affected (0.00 sec)
2.3 SELECT 列查询
1. 全列查询
使用 * 进行全列查询,通常不建议在生产环境使用,因为
- 会传输大量数据;
- 可能影响索引使用。
SELECT * FROM exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙仲谋 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
2. 指定列查询
指定列查询无需按表定义的顺序。
SELECT id, name, english FROM exam_result;
+----+-----------+---------+
| id | name | english |
+----+-----------+---------+
| 1 | 唐三藏 | 56 |
| 2 | 孙悟空 | 77 |
| 3 | 猪悟能 | 90 |
| 4 | 曹孟德 | 67 |
| 5 | 刘玄德 | 45 |
| 6 | 孙仲谋 | 78 |
| 7 | 宋公明 | 30 |
+----+-----------+---------+
7 rows in set (0.00 sec)
3. 查询字段为表达式
- 表达式不包含字段:
SELECT id, name, 10 FROM exam_result;
+----+-----------+----+
| id | name | 10 |
+----+-----------+----+
| 1 | 唐三藏 | 10 |
| 2 | 孙悟空 | 10 |
| 3 | 猪悟能 | 10 |
| 4 | 曹孟德 | 10 |
| 5 | 刘玄德 | 10 |
| 6 | 孙仲谋 | 10 |
| 7 | 宋公明 | 10 |
+----+-----------+----+
7 rows in set (0.00 sec)
- 表达式包含一个字段:
SELECT id, name, english + 10 FROM exam_result;
+----+-----------+--------------+
| id | name | english + 10 |
+----+-----------+--------------+
| 1 | 唐三藏 | 66 |
| 2 | 孙悟空 | 87 |
| 3 | 猪悟能 | 100 |
| 4 | 曹孟德 | 77 |
| 5 | 刘玄德 | 55 |
| 6 | 孙仲谋 | 88 |
| 7 | 宋公明 | 40 |
+----+-----------+--------------+
7 rows in set (0.00 sec)
- 表达式包含多个字段:
SELECT id, name, chinese + math + english FROM exam_result;
+----+-----------+--------------------------+
| id | name | chinese + math + english |
+----+-----------+--------------------------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙仲谋 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+--------------------------+
7 rows in set (0.00 sec)
4. 为查询结果指定别名
SELECT column [AS] alias_name [, column [AS] alias_name] ... FROM table_name;
SELECT id, name, chinese + math + english as 总分 FROM exam_result;
SELECT id, name, chinese + math + english 总分 FROM exam_result; -- as可以省略
+----+-----------+--------+
| id | name | 总分 |
+----+-----------+--------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙仲谋 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+--------+
7 rows in set (0.00 sec)
5. 结果去重(DISTINCT)
-- 去重数学成绩
SELECT DISTINCT math FROM exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
6 rows in set (0.01 sec)
2.4 WHERE 条件
2.4.1 各种运算符
一、比较运算符
| 运算符 | 说明 |
|---|---|
>, >=, <, <= |
大于、大于等于、小于、小于等于 |
= |
等于,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);条件为 FALSE,结果为 TRUE (1) |
2.4.2 案例演示
1. 英语不及格的同学及英语成绩(< 60)
-- 基本比较
SELECT name, english FROM exam_result WHERE english < 60;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56 |
| 刘玄德 | 45 |
| 宋公明 | 30 |
+-----------+---------+
3 rows in set (0.01 sec)
2. 语文成绩在 [80, 90] 分的同学及语文成绩
方法一:使用 AND 进行条件连接
SELECT name, chinese FROM exam_result WHERE chinese >= 80 AND chinese <= 90;
方法二:使用 BETWEEN … AND … 条件
SELECT name, chinese FROM exam_result WHERE chinese BETWEEN 80 AND 90;
两种方法结果一致:
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孙悟空 | 87 |
| 猪悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)
3. 数学成绩是 58 或 59 或 98 或 99 分的同学及数学成绩
方法一:使用 OR 进行条件连接
SELECT name, math FROM exam_result
WHERE math = 58
OR math = 59
OR math = 98
OR math = 99;
方法二:使用 IN 条件
SELECT name, math FROM exam_result WHERE math IN (58, 59, 98, 99);
两种方法结果一致:
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 猪悟能 | 98 |
+-----------+------+
2 rows in set (0.00 sec)
4. 姓孙的同学及孙某同学
1)匹配所有姓 “孙” 的同学(% 匹配任意多个字符)
SELECT name FROM exam_result WHERE name LIKE '孙%';
+-----------+
| name |
+-----------+
| 孙悟空 |
| 孙仲谋 |
+-----------+
2 rows in set (0.01 sec)
2)匹配名字为 “孙悟X” 的同学(_ 匹配严格一个字符)
SELECT name FROM exam_result WHERE name LIKE '孙悟_';
+-----------+
| name |
+-----------+
| 孙悟空 |
+-----------+
1 row in set (0.00 sec)
5. 语文成绩 > 英语成绩的同学
SELECT name, chinese, english FROM exam_result WHERE chinese > english;
+-----------+---------+---------+
| name | chinese | english |
+-----------+---------+---------+
| 唐三藏 | 67 | 56 |
| 孙悟空 | 87 | 77 |
| 曹孟德 | 82 | 67 |
| 刘玄德 | 55 | 45 |
| 宋公明 | 75 | 30 |
+-----------+---------+---------+
5 rows in set (0.00 sec)
6. 总分在 200 分以下的同学
注意:别名不能用在 WHERE 条件中
SELECT name, chinese + math + english 总分
FROM exam_result
WHERE chinese + math + english < 200;
+-----------+--------+
| name | 总分 |
+-----------+--------+
| 刘玄德 | 185 |
| 宋公明 | 170 |
+-----------+--------+
2 rows in set (0.00 sec)
补:select语句的执行顺序
mysql> select name, chinese+math+english 总分 from exam_result where 总分 < 200;
ERROR 1054 (42S22): Unknown column '总分' in 'where clause'
-- 1.from exam_result
-- 2.where 总分 < 200;
-- 3.select name, chinese+math+english 总分
7. 语文成绩 > 80 并且不姓孙的同学
SELECT id, name, chinese, math, english
FROM exam_result
WHERE chinese > 80
AND name NOT LIKE '孙%';
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
+----+-----------+---------+------+---------+
2 rows in set (0.01 sec)
8. 孙某同学,否则要求总分 > 200 并且 语文成绩 <数学成绩 并且 英语成绩> 80
- 优先级同C语言,and优先级大于or
SELECT name, chinese, math, english, chinese + math + english 总分
FROM exam_result
WHERE name LIKE '孙%'
OR (chinese + math + english > 200
AND chinese < math
AND english > 80);
+-----------+---------+------+---------+--------+
| name | chinese | math | english | 总分 |
+-----------+---------+------+---------+--------+
| 孙悟空 | 87 | 78 | 77 | 242 |
| 猪悟能 | 88 | 98 | 90 | 276 |
| 孙仲谋 | 70 | 73 | 78 | 221 |
+-----------+---------+------+---------+--------+
3 rows in set (0.00 sec)
9. NULL 的查询
1)查询 qq 已知的同学姓名
SELECT name, qq FROM students WHERE qq IS NOT NULL;
create table test(
id int,
name varchar(20)
);
mysql> insert into test (id, name) values (1, 'aa');
mysql> insert into test (id, name) values (null, 'aa');
mysql> insert into test (id, name) values (2, null);
mysql> insert into test (id, name) values (null, null);
-- 查询name为NULL
select * from test where name is null;
+------+------+
| id | name |
+------+------+
| 2 | NULL |
| NULL | NULL |
+------+------+
2 rows in set (0.00 sec)
-- 查询name不为NULL
mysql> select * from test where name is not null;
+------+------+
| id | name |
+------+------+
| 1 | aa |
| NULL | aa |
+------+------+
2 rows in set (0.00 sec)
2)NULL 和 NULL 的比较:= 与 <=> 的区别
-- NULL 不安全
SELECT NULL = NULL, NULL != 1, NULL = 0;
-- NULL <=> NULL, NULL <=> 1, NULL <=> 0
NULL = NULL→ NULLNULL <=> NULL→ 1
2.5 结果排序(ORDER BY)
2.5.1 基础语法
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC] [, ...];
注意:没有
ORDER BY子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序。
2.5.2 案例演示
1. 同学及数学成绩,按数学成绩升序显示
SELECT name, math FROM exam_result ORDER BY math; -- 默认就是ASC升序排列
SELECT name, math FROM exam_result ORDER BY math ASC;
+-----------+------+
| name | math |
+-----------+------+
| 宋公明 | 65 |
| 孙仲谋 | 73 |
| 孙悟空 | 78 |
| 曹孟德 | 84 |
| 刘玄德 | 85 |
| 唐三藏 | 98 |
| 猪悟能 | 98 |
+-----------+------+
7 rows in set (0.00 sec)
2. 同学及 qq 号,按 qq 号排序显示
- NULL 视为比任何值都小,升序出现在最上面:
(students为第一节Create种创建的表)
SELECT name, qq FROM students ORDER BY qq;
+-----------+---------+
| name | qq |
+-----------+---------+
| 许褚 | NULL |
| 刘备 | 1234560 |
| 诸葛亮 | 1234561 |
| caocao | 2345670 |
| aaa | 4567890 |
| bbb | 4567891 |
| ccc | 4567892 |
| 许攸1 | 9876540 |
+-----------+---------+
8 rows in set (0.01 sec)
- 降序时,NULL 出现在最下面:
SELECT name, qq FROM students ORDER BY qq DESC;
+-----------+---------+
| name | qq |
+-----------+---------+
| 许攸1 | 9876540 |
| ccc | 4567892 |
| bbb | 4567891 |
| aaa | 4567890 |
| caocao | 2345670 |
| 诸葛亮 | 1234561 |
| 刘备 | 1234560 |
| 许褚 | NULL |
+-----------+---------+
8 rows in set (0.00 sec)
3. 查询同学各门成绩,依次按 数学降序,英语升序,语文升序 的方式显示
多字段排序时,优先级随书写顺序:
SELECT name, math, english, chinese FROM exam_result
ORDER BY math DESC, english, chinese;
+-----------+------+---------+---------+
| name | math | english | chinese |
+-----------+------+---------+---------+
| 唐三藏 | 98 | 56 | 67 |
| 猪悟能 | 98 | 90 | 88 |
| 刘玄德 | 85 | 45 | 55 |
| 曹孟德 | 84 | 67 | 82 |
| 孙悟空 | 78 | 77 | 87 |
| 孙仲谋 | 73 | 78 | 70 |
| 宋公明 | 65 | 30 | 75 |
+-----------+------+---------+---------+
4. 查询同学及总分,由高到低
ORDER BY 中可以使用表达式,也可以使用列别名:
-- 使用表达式
SELECT name, chinese + english + math FROM exam_result
ORDER BY chinese + english + math DESC;
-- 使用别名
SELECT name, chinese + english + math 总分 FROM exam_result ORDER BY 总分 DESC;
+-----------+--------+
| name | 总分 |
+-----------+--------+
| 猪悟能 | 276 |
| 孙悟空 | 242 |
| 曹孟德 | 233 |
| 唐三藏 | 221 |
| 孙仲谋 | 221 |
| 刘玄德 | 185 |
| 宋公明 | 170 |
+-----------+--------+
7 rows in set (0.00 sec)
补:此处能使用别名排序了?
SELECT name, chinese + english + math 总分 FROM exam_result ORDER BY 总分 DESC;
-- 1.FROM exam_result
-- 2.SELECT name, chinese + english + math 总分
-- 3.ORDER BY 总分 DESC
5. 查询姓孙的同学或者姓曹的同学数学成绩,结果按数学成绩由高到低显示
结合 WHERE 和 ORDER BY:
SELECT name, math FROM exam_result
WHERE name LIKE '孙%' OR name LIKE '曹%'
ORDER BY math DESC;
-- 1.FROM exam_result
-- 2.WHERE name LIKE '孙%' OR name LIKE '曹%'
-- 3.SELECT name, math
-- 4.ORDER BY math DESC
+-----------+------+
| name | math |
+-----------+------+
| 曹孟德 | 84 |
| 孙悟空 | 78 |
| 孙仲谋 | 73 |
+-----------+------+
3 rows in set (0.01 sec)
2.6 筛选分页结果(LIMIT)
2.6.1 基础语法
起始下标为 0:
-- 从 0 开始,筛选 n 条结果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
-- 从 s 开始,筛选 n 条结果(不推荐,建议使用第二种)
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;
-- 从 s 开始,筛选 n 条结果(比第一种用法更明确,推荐)
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
建议:对未知表进行查询时,最好加一条
LIMIT 1,避免因表中数据过大,查询全表数据导致数据库卡死。
2.6.2 分页案例
按 id 分页,每页 3 条记录,分别显示第 1、2、3 页:
第 1 页
SELECT id, name, math, english, chinese FROM exam_result
ORDER BY id LIMIT 3 OFFSET 0;
+----+-----------+------+---------+---------+
| id | name | math | english | chinese |
+----+-----------+------+---------+---------+
| 1 | 唐三藏 | 98 | 56 | 67 |
| 2 | 孙悟空 | 78 | 77 | 87 |
| 3 | 猪悟能 | 98 | 90 | 88 |
+----+-----------+------+---------+---------+
3 rows in set (0.00 sec)
第 2 页
SELECT id, name, math, english, chinese FROM exam_result
ORDER BY id LIMIT 3 OFFSET 3;
+----+-----------+------+---------+---------+
| id | name | math | english | chinese |
+----+-----------+------+---------+---------+
| 4 | 曹孟德 | 84 | 67 | 82 |
| 5 | 刘玄德 | 85 | 45 | 55 |
| 6 | 孙仲谋 | 73 | 78 | 70 |
+----+-----------+------+---------+---------+
3 rows in set (0.00 sec)
第 3 页(结果不足 3 个,不会有影响)
SELECT id, name, math, english, chinese FROM exam_result
ORDER BY id LIMIT 3 OFFSET 6;
+----+-----------+------+---------+---------+
| id | name | math | english | chinese |
+----+-----------+------+---------+---------+
| 7 | 宋公明 | 65 | 30 | 75 |
+----+-----------+------+---------+---------+
1 row in set (0.01 sec)
3. Update(更新)
3.1 基础语法
UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...];
作用:对查询到的结果进行列值更新。
3.2 案例演示
1. 将孙悟空同学的数学成绩变更为 80 分
-- 查看原数据
SELECT name, math FROM exam_result WHERE name = '孙悟空';
+-----------+------+
| name | math |
+-----------+------+
| 孙悟空 | 78 |
+-----------+------+
1 row in set (0.03 sec)
-- 数据更新
UPDATE exam_result SET math = 80 WHERE name = '孙悟空';
-- Query OK, 1 row affected (0.04 sec)
-- Rows matched: 1 Changed: 1 Warnings: 0
-- 查看更新后数据
SELECT name, math FROM exam_result WHERE name = '孙悟空';
+-----------+------+
| name | math |
+-----------+------+
| 孙悟空 | 80 |
+-----------+------+
1 row in set (0.03 sec)
2. 将曹孟德同学的数学成绩变更为 60 分,语文成绩变更为 70 分
-- 查看原数据
SELECT name, math, chinese FROM exam_result WHERE name = '曹孟德';
+-----------+------+---------+
| name | math | chinese |
+-----------+------+---------+
| 曹孟德 | 84 | 82 |
+-----------+------+---------+
1 row in set (0.00 sec)
-- 数据更新(一次更新多个列)
mysql> update exam_result set math=60,chinese=70 where name='曹孟德';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 查看更新后数据
mysql> select name,math,chinese from exam_result where name = '曹孟德';
+-----------+------+---------+
| name | math | chinese |
+-----------+------+---------+
| 曹孟德 | 60 | 70 |
+-----------+------+---------+
1 row in set (0.00 sec)
3. 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
-- 查看原数据(按总分升序取前3)
mysql> select *,chinese+math+english total from exam_result order by total limit 3;
+----+-----------+---------+------+---------+-------+
| id | name | chinese | math | english | total |
+----+-----------+---------+------+---------+-------+
| 7 | 宋公明 | 75 | 65 | 30 | 170 |
| 5 | 刘玄德 | 55 | 85 | 45 | 185 |
| 4 | 曹孟德 | 70 | 60 | 67 | 197 |
+----+-----------+---------+------+---------+-------+
3 rows in set (0.00 sec)
mysql> update exam_result set math=math+30 order by chinese+math+english limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
SELECT *, chinese + math + english 总分 FROM exam_result WHERE name IN
('宋公明', '刘玄德', '曹孟德');
+----+-----------+---------+------+---------+--------+
| id | name | chinese | math | english | 总分 |
+----+-----------+---------+------+---------+--------+
| 4 | 曹孟德 | 70 | 90 | 67 | 227 |
| 5 | 刘玄德 | 55 | 115 | 45 | 215 |
| 7 | 宋公明 | 75 | 95 | 30 | 200 |
+----+-----------+---------+------+---------+--------+
3 rows in set (0.00 sec)
SELECT *, chinese + math + english 总分 FROM exam_result ORDER BY 分
LIMIT 3;
+----+-----------+---------+------+---------+--------+
| id | name | chinese | math | english | 总分 |
+----+-----------+---------+------+---------+--------+
| 7 | 宋公明 | 75 | 95 | 30 | 200 |
| 5 | 刘玄德 | 55 | 115 | 45 | 215 |
| 1 | 唐三藏 | 67 | 98 | 56 | 221 |
+----+-----------+---------+------+---------+--------+
3 rows in set (0.00 sec)
4. 将所有同学的语文成绩更新为原来的 2 倍
注意:更新全表的语句慎用!没有
WHERE子句时,会更新全表。
-- 查看原数据
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 80 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 70 | 90 | 67 |
| 5 | 刘玄德 | 55 | 115 | 45 |
| 6 | 孙仲谋 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 95 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
-- 数据更新
mysql> update exam_result set chinese=chinese*2;
Query OK, 7 rows affected (0.01 sec)
Rows matched: 7 Changed: 7 Warnings: 0
-- 查看更新后数据
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 134 | 98 | 56 |
| 2 | 孙悟空 | 174 | 80 | 77 |
| 3 | 猪悟能 | 176 | 98 | 90 |
| 4 | 曹孟德 | 140 | 90 | 67 |
| 5 | 刘玄德 | 110 | 115 | 45 |
| 6 | 孙仲谋 | 140 | 73 | 78 |
| 7 | 宋公明 | 150 | 95 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
4. Delete(删除)
4.1 删除数据
4.1.1 语法
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
4.1.2 案例
1. 删除孙悟空同学的考试成绩
-- 查看原数据
mysql> select * from exam_result where name='孙悟空';
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 2 | 孙悟空 | 174 | 80 | 77 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)
-- 删除数据
mysql> delete from exam_result where name='孙悟空';
Query OK, 1 row affected (0.01 sec)
-- 查看删除结果
mysql> select * from exam_result where name='孙悟空';
Empty set (0.00 sec)
2. 删除整张表数据
注意:删除整表操作要慎用!
-- 准备测试表
CREATE TABLE for_delete (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20)
);
-- 插入测试数据
INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
-- 查看测试数据
SELECT * FROM for_delete;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
-- 删除整表数据
DELETE FROM for_delete;
-- Query OK, 3 rows affected (0.00 sec)
-- 查看删除结果
SELECT * FROM for_delete;
-- Empty set (0.00 sec)
-- 再插入一条数据,自增 id 在原值上增长
INSERT INTO for_delete (name) VALUES ('D');
SELECT * FROM for_delete;
+----+------+
| id | name |
+----+------+
| 4 | D |
+----+------+
1 row in set (0.00 sec)
-- 查看表结构,会有 AUTO_INCREMENT=5
SHOW CREATE TABLE for_delete\G
4.2 截断表
语法
TRUNCATE [TABLE] table_name
注意
- 只能对整表操作,不能像
DELETE一样针对部分数据操作; - 实际上 MySQL 不对数据操作,所以比
DELETE更快,但是TRUNCATE在删除数据的时候,并不经过真正的事务,所以无法回滚; - 会重置
AUTO_INCREMENT项。
案例
-- 准备测试表
CREATE TABLE for_truncate (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20)
);
-- 插入测试数据
INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C');
-- 查看测试数据
SELECT * FROM for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
-- 截断整表数据,注意影响行数是 0,所以实际上没有对数据真正操作
TRUNCATE for_truncate;
-- Query OK, 0 rows affected (0.10 sec)
-- 查看删除结果
SELECT * FROM for_truncate;
-- Empty set (0.00 sec)
-- 再插入一条数据,自增 id 在重新增长
INSERT INTO for_truncate (name) VALUES ('D');
SELECT * FROM for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | D |
+----+------+
1 row in set (0.00 sec)
-- 查看表结构,会有 AUTO_INCREMENT=2
SHOW CREATE TABLE for_truncate\G
5. 插入查询结果
语法
INSERT INTO table_name [(column [, column ...])] SELECT ...
案例:删除表中的重复记录,重复的数据只能有一份
-- 创建原数据表
CREATE TABLE duplicate_table (id int, name varchar(20));
-- 插入测试数据
INSERT INTO duplicate_table VALUES
(100, 'aaa'),
(100, 'aaa'),
(200, 'bbb'),
(200, 'bbb'),
(300, 'ccc'),
(300, 'ccc');
-- 创建一张空表 no_duplicate_table,结构和 duplicate_table 一样
CREATE TABLE no_duplicate_table LIKE duplicate_table;
-- 将 duplicate_table 的去重数据插入到 no_duplicate_table
INSERT INTO no_duplicate_table SELECT DISTINCT * FROM duplicate_table;
-- Query OK, 3 rows affected (0.00 sec)
-- 通过重命名表,实现原子的去重操作
RENAME TABLE duplicate_table TO old_duplicate_table,
no_duplicate_table TO duplicate_table;
-- 查看最终结果
SELECT * FROM duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
3 rows in set (0.00 sec)
6. 聚合函数
6.1 函数列表
| 函数 | 说明 |
|---|---|
| COUNT([DISTINCT] expr) | 返回查询到的数据的数量,NULL 不计入结果 |
| SUM([DISTINCT] expr) | 返回查询到的数据的总和,不是数字没有意义 |
| AVG([DISTINCT] expr) | 返回查询到的数据的平均值,不是数字没有意义 |
| MAX([DISTINCT] expr) | 返回查询到的数据的最大值,不是数字没有意义 |
| MIN([DISTINCT] expr) | 返回查询到的数据的最小值,不是数字没有意义 |
6.2 案例演示
1. 统计班级共有多少同学
- 使用
*做统计,不受NULL影响:
SELECT COUNT(*) from exam_result;
+----------+
| COUNT(*) |
+----------+
| 6 |
+----------+
1 row in set (0.01 sec)
2. 统计本次考试的数学成绩分数个数
COUNT(math)统计的是全部成绩:
SELECT COUNT(math) FROM exam_result;
+-------------+
| COUNT(math) |
+-------------+
| 6 |
+-------------+
1 row in set (0.00 sec)
COUNT(DISTINCT math)统计的是去重成绩数量:
SELECT COUNT(DISTINCT math) FROM exam_result;
+----------------------+
| COUNT(DISTINCT math) |
+----------------------+
| 5 |
+----------------------+
1 row in set (0.01 sec)
3. 统计数学成绩总分
SELECT SUM(math) FROM exam_result;
+-----------+
| SUM(math) |
+-----------+
| 569 |
+-----------+
1 row in set (0.00 sec)
- (< 80)的总分,没有结果时返回
NULL:
SELECT SUM(math) FROM exam_result WHERE math < 80;
+-----------+
| SUM(math) |
+-----------+
| 73 |
+-----------+
1 row in set (0.00 sec)
4. 统计平均总分
SELECT AVG(chinese + math + english) 平均总分 FROM exam_result;
+--------------+
| 平均总分 |
+--------------+
| 297.5 |
+--------------+
1 row in set (0.01 sec)
5. 返回英语最高分
SELECT MAX(english) FROM exam_result;
+--------------+
| MAX(english) |
+--------------+
| 90 |
+--------------+
1 row in set (0.00 sec)
6 . 返回 > 70 分以上的数学最低分
SELECT MIN(math) FROM exam_result WHERE math > 70;
+-----------+
| MIN(math) |
+-----------+
| 73 |
+-----------+
1 row in set (0.00 sec)
7. GROUP BY 子句的使用
在 SELECT 中使用 GROUP BY 子句可以对指定列进行分组查询。
语法
SELECT column1, column2, ... FROM table GROUP BY column;
案例:雇员信息表(来自 Oracle 9i 的经典测试表)
- 准备表:
EMP员工表、DEPT部门表、SALGRADE工资等级表。
- 导入信息表
-- 导入信息表库
source /home/gyy/scott_data.sql
-- 使用信息表库
use scott;
Database changed
-- 查看信息表库
show tables;
+-----------------+
| Tables_in_scott |
+-----------------+
| dept |
| emp |
| salgrade |
+-----------------+
3 rows in set (0.00 sec)
- 显示每个部门的平均工资和最高工资
SELECT deptno, AVG(sal), MAX(sal) FROM emp GROUP BY deptno;
- 显示每个部门的每种岗位的平均工资和最低工资
SELECT AVG(sal), MIN(sal), job, deptno FROM emp GROUP BY deptno, job;
- 显示平均工资低于 2000 的部门和它的平均工资
SELECT AVG(sal) AS myavg FROM emp GROUP BY deptno HAVING myavg < 2000;
HAVING和GROUP BY配合使用,对GROUP BY结果进行过滤,作用类似WHERE,但WHERE不能用于聚合结果过滤。
- 显示平均工资低于 2000 的部门和它的平均工资 SMITH员工不参与统计
select deptno,job,avg(sal) myavg from emp where ename != 'SMITH' group by deptno,job having myavg < 2000;
+--------+----------+-------------+
| deptno | job | myavg |
+--------+----------+-------------+
| 30 | SALESMAN | 1400.000000 |
| 20 | CLERK | 1100.000000 |
| 30 | CLERK | 950.000000 |
| 10 | CLERK | 1300.000000 |
+--------+----------+-------------+
4 rows in set (0.03 sec)
-- 1.from emp
-- 2.where ename != 'SMITH'
-- 3.group by deptno,job
-- 4.select deptno,job,avg(sal) myavg
-- 5.having myavg < 2000;
- where和having都属于条件筛选,条件筛选的阶段是不同的。
8. 实战 SQL
- 批量插入数据
- 找出所有员工当前(
to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按逆序排序 - 查找薪水涨幅超过 15 次的员工号
emp_no以及对应的涨幅次数 - 查找所有部门当前
manager的最新薪水情况,给出dept_no,emp_no以及salary(当前表示to_date='9999-01-01') - 从
dept_emp表获取按照dept_no分组,每组个数大于等于 2,给出dept_no以及对应的数目
SQL 查询中各关键字的执行先后顺序
FROM > JOIN > WHERE > GROUP BY > WITH > HAVING > SELECT > DISTINCT > ORDER BY > LIMIT
更多推荐




所有评论(0)