MySQL-----表的增删查改
文章目录
- 前言
- 一、create
-
- 1. 单行数据 + 全列插入
- 2. 多行数据 + 指定列插入
- 3. 插入冲突否则更新
- 4. 替换
- 二、retrieve
-
- 1. select列
-
- 1.1 全列查询
- 1.2 指定列查询
- 1.3 查询字段为表达式
- 1.4 为查询结果指定别名
- 1.5 结果去重
- 2. where条件
- 3. 结果排序
-
- 3.1 同学及数学成绩,按数学成绩升序显示
- 3.2 同学及 qq 号,按 qq 号排序显示
- 3.3 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
- 3.4 查询同学及总分,由高到低
- 3.5 查询姓郭的同学或者姓洋的同学数学成绩,结果按数学成绩由高到低显示
- 4. 筛选分页结果
- 四、Update
-
- 1. 将郭雨妍同学的数学成绩变更为 80 分
- 2. 将洋同学的数学成绩变更为 60 分,语文成绩变更为 70 分
- 3. 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
- 4. 将所有同学的语文成绩更新为原来的 2 倍
- 四、Delete
-
- 1. 删除数据
-
- 1.1 删除郭雨妍同学的考试成绩
- 2.2 删除整张表数据
- 2. 截断表
- 五、插入查询结果
- 六、聚合函数
-
- 1. 统计班级共有多少同学
- 2. 统计班级收集的 qq 号有多少
- 3.统计本次考试的数学成绩分数个数
- 4.统计数学成绩总分
- 5.统计平均总分
- 6.返回英语最高分
- 7.返回 > 70 分以上的数学最低分
- 七、group by子句的使用
- 1.查看表结构
- 2. 如何显示每个部门的平均工资和最高工资
- 3. 显示每个部门的每种岗位的平均工资和最低工资
- 4.显示平均工资低于2000的部门和它的平均工资
- 总结
前言
CURD : create(创建),retrieve(读取),update(更新),delete(删除).
正文开始!!!
一、create
语法
INSERT [INTO] table_name[(column [, column] ...)]VALUES (value_list) [, (value_list)] ...value_list: value, [, value] ...
案例:
# 创建一张学生表
mysql> create table if not exists students(-> id int unsigned primary key auto_increment,-> sn int unsigned unique key not null comment '学生的学号',-> name varchar(64) not null comment '学生的姓名',-> qq varchar(20) unique key-> );
Query OK, 0 rows affected (0.02 sec)
1. 单行数据 + 全列插入
-- 插入两条记录,value_list 数量必须和定义表的列的数量及顺序一致
-- 注意,这里在插入的时候,也可以不用指定id(当然,那时候就需要明确插入数据到那些列了),那么mysql会使用默认
的值进行自增。
mysql> insert into students values (1,61,'路飞','111@qq.com');
Query OK, 1 row affected (0.00 sec)mysql> insert into students values (2,6,'索隆',null);
Query OK, 1 row affected (0.01 sec)-- 查看插入结果
mysql> select * from students;
+----+----+--------+------------+
| id | sn | name | qq |
+----+----+--------+------------+
| 1 | 61 | 路飞 | 111@qq.com |
| 2 | 6 | 索隆 | NULL |
+----+----+--------+------------+
2 rows in set (0.00 sec
2. 多行数据 + 指定列插入
-- 插入三条记录,value_list 数量必须和指定列数量及顺序一致
mysql> insert into students (sn,name,qq) values (62,'乌索普','222@qq.com'),-> (63,'娜美','333@qq.com'),-> (64,'乔巴',null);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0-- 查看插入结果
mysql> select * from students;
+----+----+-----------+------------+
| id | sn | name | qq |
+----+----+-----------+------------+
| 1 | 61 | 路飞 | 111@qq.com |
| 2 | 6 | 索隆 | NULL |
| 3 | 62 | 乌索普 | 222@qq.com |
| 4 | 63 | 娜美 | 333@qq.com |
| 5 | 64 | 乔巴 | NULL |
+----+----+-----------+------------+
5 rows in set (0.00 sec)
3. 插入冲突否则更新
由于 主键 和 唯一键 对于的值已经存在而导致插入失败
-- 主键冲突
mysql> insert into students (id,sn,name) values (1,66,'弗兰奇');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'-- 唯一键冲突
mysql> insert into students (id,sn,name) values (10,6,'弗兰奇');
ERROR 1062 (23000): Duplicate entry '6' for key 'sn'
可以选择性的进行同步更新操作语法:
INSERT ... ON DUPLICATE KEY UPDATE
column = value [, column = value] ...
mysql> select * from students;
+----+----+-----------+------------+
| id | sn | name | qq |
+----+----+-----------+------------+
| 1 | 61 | 路飞 | 111@qq.com |
| 2 | 6 | 索隆 | NULL |
| 3 | 62 | 乌索普 | 222@qq.com |
| 4 | 63 | 娜美 | 333@qq.com |
| 5 | 64 | 乔巴 | NULL |
+----+----+-----------+------------+
5 rows in set (0.00 sec)mysql> insert into students (id,sn,name) values (2,66,'索隆') on duplicate key update sn=65,name='索隆';
Query OK, 2 rows affected (0.00 sec)-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新mysql> select ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)mysql> select * from students;
+----+----+-----------+------------+
| id | sn | name | qq |
+----+----+-----------+------------+
| 1 | 61 | 路飞 | 111@qq.com |
| 2 | 65 | 索隆 | NULL |
| 3 | 62 | 乌索普 | 222@qq.com |
| 4 | 63 | 娜美 | 333@qq.com |
| 5 | 64 | 乔巴 | NULL |
+----+----+-----------+------------+
5 rows in set (0.00 sec)
-- ON DUPLICATE KEY 当发生重复key的时候
4. 替换
-- 主键 或者 唯一键 没有冲突,则直接插入;
-- 主键 或者 唯一键 如果冲突,则删除后再插入-- 主键和唯一键都冲突了
mysql> replace into students (id,sn,name) values (2,62,'索隆');
Query OK, 3 rows affected (0.01 sec)mysql> select * from students;
+----+----+--------+------------+
| id | sn | name | qq |
+----+----+--------+------------+
| 1 | 61 | 路飞 | 111@qq.com |
| 2 | 62 | 索隆 | NULL |
| 4 | 63 | 娜美 | 333@qq.com |
| 5 | 64 | 乔巴 | NULL |
+----+----+--------+------------+
4 rows in set (0.00 sec)-- 1 row affected: 表中没有冲突数据,数据被插入
-- n row affected: 表中有冲突数据,删除后重新插入
二、retrieve
语法:
SELECT[DISTINCT] {* | {column [, column] ...}[FROM table_name][WHERE ...][ORDER BY column [ASC | DESC], ...]LIMIT ...
案例:
-- 创建表结构
mysql> 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 '英语成绩'-> );
Query OK, 0 rows affected (0.01 sec)-- 插入测试数据
mysql> 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.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
1. select列
1.1 全列查询
-- 通常情况下不建议使用 * 进行全列查询
-- 1. 查询的列越多,意味着需要传输的数据量越大;
-- 2. 可能会影响到索引的使用。(索引待后面课程讲解)
mysql> 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)
1.2 指定列查询
-- 指定列的顺序不需要按定义表的顺序来mysql> select name,id,math from exam_result;
+-----------+----+------+
| name | id | math |
+-----------+----+------+
| 赖益烨 | 1 | 98 |
| 郭雨妍 | 2 | 78 |
| 马师傅 | 3 | 98 |
| 洋妈 | 4 | 84 |
| 李观洋 | 5 | 85 |
| 郭昊 | 6 | 73 |
| 张照洋 | 7 | 65 |
+-----------+----+------+
7 rows in set (0.00 sec)
1.3 查询字段为表达式
-- 表达式不包含字段
mysql> 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)mysql> SELECT id, name, 10+10 FROM exam_result;
+----+-----------+-------+
| id | name | 10+10 |
+----+-----------+-------+
| 1 | 赖益烨 | 20 |
| 2 | 郭雨妍 | 20 |
| 3 | 马师傅 | 20 |
| 4 | 洋妈 | 20 |
| 5 | 李观洋 | 20 |
| 6 | 郭昊 | 20 |
| 7 | 张照洋 | 20 |
+----+-----------+-------+
7 rows in set (0.00 sec)mysql> 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)mysql> select id,name,chinese+50 from exam_result;
+----+-----------+------------+
| id | name | chinese+50 |
+----+-----------+------------+
| 1 | 赖益烨 | 117 |
| 2 | 郭雨妍 | 137 |
| 3 | 马师傅 | 138 |
| 4 | 洋妈 | 132 |
| 5 | 李观洋 | 105 |
| 6 | 郭昊 | 120 |
| 7 | 张照洋 | 125 |
+----+-----------+------------+
7 rows in set (0.00 sec)
1.4 为查询结果指定别名
语法:
SELECT column [AS] alias_name [...] FROM table_name;
mysql> select id,name,chinese+math+english 总分 from exam_result;
+----+-----------+--------+
| id | name | 总分 |
+----+-----------+--------+
| 1 | 赖益烨 | 221 |
| 2 | 郭雨妍 | 242 |
| 3 | 马师傅 | 276 |
| 4 | 洋妈 | 233 |
| 5 | 李观洋 | 185 |
| 6 | 郭昊 | 221 |
| 7 | 张照洋 | 170 |
+----+-----------+--------+
7 rows in set (0.00 sec)
1.5 结果去重
mysql> select math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 98 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
7 rows in set (0.00 sec)
# 98分重复啦mysql> select distinct math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
6 rows in set (0.00 sec)
2. 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) |
案例:
2.1 英语不及格的同学及英语成绩 ( < 60 )
mysql> select name,english from exam_result where english<60;
+-----------+---------+
| name | english |
+-----------+---------+
| 赖益烨 | 56 |
| 李观洋 | 45 |
| 张照洋 | 30 |
+-----------+---------+
3 rows in set (0.00 sec)
2.2 语文成绩在 [80, 90] 分的同学及语文成绩
-- 使用 AND 进行条件连接
mysql> select name,chinese from exam_result where chinese>=80 and chinese<=90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 郭雨妍 | 87 |
| 马师傅 | 88 |
| 洋妈 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)-- 使用 BETWEEN ... AND ... 条件
mysql> select name,chinese from exam_result where chinese between 80 and 90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 郭雨妍 | 87 |
| 马师傅 | 88 |
| 洋妈 | 82 |
+-----------+---------+
3 rows in set (0.01 sec)
2.3 数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
-- 使用 OR 进行条件连接
mysql> select name,math from exam_result where math=58 or math=59 or math=98 or math=99;
+-----------+------+
| name | math |
+-----------+------+
| 赖益烨 | 98 |
| 马师傅 | 98 |
+-----------+------+
2 rows in set (0.00 sec)-- 使用 IN 条件
mysql> select name,math from exam_result where math in (58,59,98,99);
+-----------+------+
| name | math |
+-----------+------+
| 赖益烨 | 98 |
| 马师傅 | 98 |
+-----------+------+
2 rows in set (0.00 sec)
2.4 姓郭的同学 及 郭某同学
-- % 匹配任意多个(包括 0 个)任意字符
mysql> select name from exam_result where name like '郭%';
+-----------+
| name |
+-----------+
| 郭雨妍 |
| 郭昊 |
+-----------+
2 rows in set (0.00 sec)-- _ 匹配严格的一个任意字符
mysql> select name from exam_result where name like '郭_';
+--------+
| name |
+--------+
| 郭昊 |
+--------+
1 row in set (0.00 sec)mysql> select name from exam_result where name like '郭__';
+-----------+
| name |
+-----------+
| 郭雨妍 |
+-----------+
1 row in set (0.00 sec)
2.5 语文成绩好于英语成绩的同学
-- WHERE 条件中比较运算符两侧都是字段
mysql> 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)
2.6 总分在 200 分以下的同学
-- WHERE 条件中使用表达式
-- 别名不能用在 WHERE 条件中
mysql> select name,chinese+math+english total from exam_result where chinese+math+english<200;
+-----------+-------+
| name | total |
+-----------+-------+
| 李观洋 | 185 |
| 张照洋 | 170 |
+-----------+-------+
2 rows in set (0.00 sec)
2.7 语文成绩 > 80 并且不姓孙的同学
-- AND 与 NOT 的使用
mysql> select name,chinese from exam_result where chinese>80 and name not like '孙%';
+-----------+---------+
| name | chinese |
+-----------+---------+
| 郭雨妍 | 87 |
| 马师傅 | 88 |
| 洋妈 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)
2.8 郭某同学,否则要求总成绩 > 200 并且 语文成绩 < 数学成绩 并且 英语成绩 > 80
-- 综合性查询
mysql> select *,chinese+math+english total from exam_result where (name like '郭_') or (chinese+math+english>200 and chinese<math and english>80);
+----+-----------+---------+------+---------+-------+
| id | name | chinese | math | english | total |
+----+-----------+---------+------+---------+-------+
| 3 | 马师傅 | 88 | 98 | 90 | 276 |
| 6 | 郭昊 | 70 | 73 | 78 | 221 |
+----+-----------+---------+------+---------+-------+
2 rows in set (0.00 sec)
2.9 NULL 的查询
-- 查询 students 表
mysql> select * from students where qq is null;
+----+----+--------+------+
| id | sn | name | qq |
+----+----+--------+------+
| 2 | 62 | 索隆 | NULL |
| 5 | 64 | 乔巴 | NULL |
+----+----+--------+------+
2 rows in set (0.00 sec)mysql> select * from students where qq<=> null;
+----+----+--------+------+
| id | sn | name | qq |
+----+----+--------+------+
| 2 | 62 | 索隆 | NULL |
| 5 | 64 | 乔巴 | NULL |
+----+----+--------+------+
2 rows in set (0.00 sec)-- 查询 qq 号已知的同学姓名
mysql> select * from students where qq is not null;
+----+----+--------+------------+
| id | sn | name | qq |
+----+----+--------+------------+
| 1 | 61 | 路飞 | 111@qq.com |
| 4 | 63 | 娜美 | 333@qq.com |
+----+----+--------+------------+
2 rows in set (0.00 sec)-- NULL 和 NULL 的比较,= 和 <=> 的区别
mysql> SELECT NULL = NULL, NULL = 1, NULL = 0;
+-------------+----------+----------+
| NULL = NULL | NULL = 1 | NULL = 0 |
+-------------+----------+----------+
| NULL | NULL | NULL |
+-------------+----------+----------+
1 row in set (0.00 sec)mysql> SELECT NULL <=> NULL, NULL <=> 1, NULL <=> 0;
+---------------+------------+------------+
| NULL <=> NULL | NULL <=> 1 | NULL <=> 0 |
+---------------+------------+------------+
| 1 | 0 | 0 |
+---------------+------------+------------+
1 row in set (0.00 sec)
如何理解mysql在当前的select执行的时候,是怎么执行的???
3. 结果排序
语法:
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]ORDER BY column [ASC|DESC], [...];
注意 : 没有order by 子句的查询,返回的顺序是未定义的,永远不要依赖这个顺序!!!
案例:
3.1 同学及数学成绩,按数学成绩升序显示
mysql> 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)
3.2 同学及 qq 号,按 qq 号排序显示
mysql> select * from students;
+----+----+-----------+------------+
| id | sn | name | qq |
+----+----+-----------+------------+
| 1 | 61 | 路飞 | 111@qq.com |
| 2 | 62 | 索隆 | NULL |
| 4 | 63 | 娜美 | 333@qq.com |
| 5 | 64 | 乔巴 | NULL |
| 6 | 65 | 弗兰奇 | 444@qq.com |
| 7 | 66 | 罗宾 | 555@qq.com |
| 8 | 67 | 布鲁克 | 123@qq.com |
+----+----+-----------+------------+
7 rows in set (0.00 sec)# 顺序
-- NULL 视为比任何值都小,升序出现在最上面
mysql> select name,qq from students order by qq asc;
+-----------+------------+
| name | qq |
+-----------+------------+
| 索隆 | NULL |
| 乔巴 | NULL |
| 路飞 | 111@qq.com |
| 布鲁克 | 123@qq.com |
| 娜美 | 333@qq.com |
| 弗兰奇 | 444@qq.com |
| 罗宾 | 555@qq.com |
+-----------+------------+
7 rows in set (0.00 sec)# 逆序
-- NULL 视为比任何值都小,降序出现在最下面
mysql> select name,qq from students order by qq desc;
+-----------+------------+
| name | qq |
+-----------+------------+
| 罗宾 | 555@qq.com |
| 弗兰奇 | 444@qq.com |
| 娜美 | 333@qq.com |
| 布鲁克 | 123@qq.com |
| 路飞 | 111@qq.com |
| 索隆 | NULL |
| 乔巴 | NULL |
+-----------+------------+
7 rows in set (0.00 sec)
3.3 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
-- 多字段排序,排序优先级随书写顺序
mysql> select name,math,english,chinese from exam_result order by math desc,english asc,chinese asc;
+-----------+------+---------+---------+
| 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 |
+-----------+------+---------+---------+
7 rows in set (0.00 sec)
3.4 查询同学及总分,由高到低
-- ORDER BY 中可以使用表达式
mysql> select name,chinese+math+english total from exam_result order by chinese+math+english desc;
+-----------+-------+
| name | total |
+-----------+-------+
| 马师傅 | 276 |
| 郭雨妍 | 242 |
| 洋妈 | 233 |
| 赖益烨 | 221 |
| 郭昊 | 221 |
| 李观洋 | 185 |
| 张照洋 | 170 |
+-----------+-------+
7 rows in set (0.00 sec)-- ORDER BY 子句中可以使用列别名
mysql> select name,chinese+math+english total from exam_result order by total desc;
+-----------+-------+
| name | total |
+-----------+-------+
| 马师傅 | 276 |
| 郭雨妍 | 242 |
| 洋妈 | 233 |
| 赖益烨 | 221 |
| 郭昊 | 221 |
| 李观洋 | 185 |
| 张照洋 | 170 |
+-----------+-------+
7 rows in set (0.00 sec)
3.5 查询姓郭的同学或者姓洋的同学数学成绩,结果按数学成绩由高到低显示
-- 结合 WHERE 子句 和 ORDER BY 子句mysql> select name,math from exam_result where name like '郭%' or name like '洋%' order by math desc;
+-----------+------+
| name | math |
+-----------+------+
| 洋妈 | 84 |
| 郭雨妍 | 78 |
| 郭昊 | 73 |
+-----------+------+
3 rows in set (0.00 sec)
4. 筛选分页结果
语法:
-- 起始下标为 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,避免因为表中数据过大,查询全表数据导致数据库卡死!!!
案例:
mysql> select name,math from exam_result limit 3;
+-----------+------+
| name | math |
+-----------+------+
| 赖益烨 | 98 |
| 郭雨妍 | 78 |
| 马师傅 | 98 |
+-----------+------+
3 rows in set (0.00 sec)mysql> select name,math from exam_result limit 3,3;
+-----------+------+
| name | math |
+-----------+------+
| 洋妈 | 84 |
| 李观洋 | 85 |
| 郭昊 | 73 |
+-----------+------+
3 rows in set (0.00 sec)mysql> select name,math from exam_result limit 3 offset 6;
+-----------+------+
| name | math |
+-----------+------+
| 张照洋 | 65 |
+-----------+------+
1 row in set (0.00 sec)mysql> select name,math from exam_result limit 3 offset 0;
+-----------+------+
| name | math |
+-----------+------+
| 赖益烨 | 98 |
| 郭雨妍 | 78 |
| 马师傅 | 98 |
+-----------+------+
3 rows in set (0.00 sec)
按id进行分页,每页3条记录,分别显示 第1,2,3 页!!!
-- 第 1 页
mysql> 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 页
mysql> 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 个,不会有影响
mysql> 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.00 sec)
四、Update
语法:
UPDATE table_name SET column = expr [, column = expr ...][WHERE ...] [ORDER BY ...] [LIMIT ...]
对查询到的结果进行列值更新
案例:
1. 将郭雨妍同学的数学成绩变更为 80 分
-- 更新值为具体值-- 查看原数据
mysql> select name,math from exam_result where name='郭雨妍';
+-----------+------+
| name | math |
+-----------+------+
| 郭雨妍 | 78 |
+-----------+------+
1 row in set (0.00 sec)-- 数据更新
mysql> update exam_result set math=80 where name='郭雨妍';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0-- 查看更新后数据
mysql> select name,math from exam_result where name='郭雨妍';
+-----------+------+
| name | math |
+-----------+------+
| 郭雨妍 | 80 |
+-----------+------+
1 row in set (0.01 sec)
2. 将洋同学的数学成绩变更为 60 分,语文成绩变更为 70 分
-- 一次更新多个列-- 查看原数据
mysql> 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 分
-- 更新值为原值基础上变更-- 查看原数据
-- 别名可以在ORDER BY中使用
mysql> select name,math,chinese+math+english total from exam_result order by total asc limit 3;
+-----------+------+-------+
| name | math | total |
+-----------+------+-------+
| 张照洋 | 65 | 170 |
| 李观洋 | 85 | 185 |
| 洋妈 | 60 | 197 |
+-----------+------+-------+
3 rows in set (0.00 sec)-- 数据更新,不支持 math += 30 这种语法
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-- 查看更新后数据
-- 思考:这里还可以按总分升序排序取前 3 个么?
mysql> select name,math,chinese+math+english total from exam_result where name in ('张照洋','李观洋','洋妈');
+-----------+------+-------+
| name | math | total |
+-----------+------+-------+
| 洋妈 | 90 | 227 |
| 李观洋 | 115 | 215 |
| 张照洋 | 95 | 200 |
+-----------+------+-------+
3 rows in set (0.00 sec)
# 不可以,因为数学成绩+30分以后,成绩就不一定在前三名了!!!-- 按总成绩排序后查询结果
mysql> select name,math,chinese+math+english total from exam_result order by total asc limit 3;
+-----------+------+-------+
| name | math | total |
+-----------+------+-------+
| 张照洋 | 95 | 200 |
| 李观洋 | 115 | 215 |
| 赖益烨 | 98 | 221 |
+-----------+------+-------+
3 rows in set (0.00 sec)
4. 将所有同学的语文成绩更新为原来的 2 倍
-- 没有 WHERE 子句,则更新全表-- 查看原数据
mysql> select name,chinese from exam_result;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 赖益烨 | 67 |
| 郭雨妍 | 87 |
| 马师傅 | 88 |
| 洋妈 | 70 |
| 李观洋 | 55 |
| 郭昊 | 70 |
| 张照洋 | 75 |
+-----------+---------+
7 rows in set (0.00 sec)-- 数据更新
mysql> update exam_result set chinese=chinese*2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0-- 查看更新后数据
mysql> select name,chinese from exam_result;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 赖益烨 | 134 |
| 郭雨妍 | 174 |
| 马师傅 | 176 |
| 洋妈 | 140 |
| 李观洋 | 110 |
| 郭昊 | 140 |
| 张照洋 | 150 |
+-----------+---------+
7 rows in set (0.00 sec)
四、Delete
1. 删除数据
语法:
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
案例:
1.1 删除郭雨妍同学的考试成绩
-- 查看原数据
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)-- 删除数据
mysql> delete from exam_result where name='郭雨妍';
Query OK, 1 row affected (0.00 sec)-- 查看删除结果
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 赖益烨 | 134 | 98 | 56 |
| 3 | 马师傅 | 176 | 98 | 90 |
| 4 | 洋妈 | 140 | 90 | 67 |
| 5 | 李观洋 | 110 | 115 | 45 |
| 6 | 郭昊 | 140 | 73 | 78 |
| 7 | 张照洋 | 150 | 95 | 30 |
+----+-----------+---------+------+---------+
6 rows in set (0.00 sec)
2.2 删除整张表数据
注意:删除整表操作要慎用!
-- 查看测试数据
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 赖益烨 | 134 | 98 | 56 |
| 3 | 马师傅 | 176 | 98 | 90 |
| 4 | 洋妈 | 140 | 90 | 67 |
| 5 | 李观洋 | 110 | 115 | 45 |
| 6 | 郭昊 | 140 | 73 | 78 |
| 7 | 张照洋 | 150 | 95 | 30 |
+----+-----------+---------+------+---------+
6 rows in set (0.00 sec)-- 删除整表数据
mysql> delete from exam_result;
Query OK, 6 rows affected (0.00 sec)-- 查看删除结果
mysql> select * from exam_result;
Empty set (0.00 sec)-- 再插入一条数据,自增 id 在原值上增长
mysql> insert into exam_result (name,chinese,math,english) values ('魏师傅',111,90,78);
Query OK, 1 row affected (0.00 sec)-- 查看数据
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 8 | 魏师傅 | 111 | 90 | 78 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)-- 查看表结构,会有 AUTO_INCREMENT=n 项
mysql> show create table exam_result\\G
*************************** 1. row ***************************Table: exam_result
Create Table: CREATE TABLE `exam_result` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(20) NOT NULL COMMENT '同学姓名',`chinese` float DEFAULT '0' COMMENT '语文成绩',`math` float DEFAULT '0' COMMENT '数学成绩',`english` float DEFAULT '0' COMMENT '英语成绩',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
2. 截断表
语法:
TRUNCATE [TABLE] table_name
注意 : 这个操作慎用
- 只能对整表操作,不能像delete一样针对部分数据操作.
- 实际上MySQL不对数据操作,所以比delete更快,但是truncate在删除数据的时候,并不经过真正的事务,所以无法回滚.
- 会重置 auot_increment项.
案例:
-- 查看测试数据
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 9 | 赖益烨 | 67 | 98 | 56 |
| 10 | 郭雨妍 | 87 | 78 | 77 |
| 11 | 马师傅 | 88 | 98 | 90 |
| 12 | 洋妈 | 82 | 84 | 67 |
| 13 | 李观洋 | 55 | 85 | 45 |
| 14 | 郭昊 | 70 | 73 | 78 |
| 15 | 张照洋 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)-- 截断整表数据,注意影响行数是 0,所以实际上没有对数据真正操作
mysql> truncate exam_result;
Query OK, 0 rows affected (0.01 sec)-- 查看删除结果
mysql> select * from exam_result;
Empty set (0.00 sec)-- 再插入一条数据,自增 id 在重新增长
mysql> insert into exam_result (name,chinese,math,english) values ('魏师傅',111,90,78);
Query OK, 1 row affected (0.00 sec)-- 查看数据
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 魏师傅 | 111 | 90 | 78 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)-- 查看表结构,会有 AUTO_INCREMENT=2 项
mysql> show create table exam_result\\G
*************************** 1. row ***************************Table: exam_result
Create Table: CREATE TABLE `exam_result` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(20) NOT NULL COMMENT '同学姓名',`chinese` float DEFAULT '0' COMMENT '语文成绩',`math` float DEFAULT '0' COMMENT '数学成绩',`english` float DEFAULT '0' COMMENT '英语成绩',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
五、插入查询结果
语法:
INSERT INTO table_name [(column [, column ...])] SELECT ...
案例:
-- 创建原数据表
mysql> create table duplicate_table(id int,name varchar(20));
Query OK, 0 rows affected (0.02 sec)mysql> INSERT INTO duplicate_table VALUES-> (100, 'aaa'),-> (100, 'aaa'),-> (200, 'bbb'),-> (200, 'bbb'),-> (200, 'bbb'),-> (300, 'ccc');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0-- 创建一张空表 no_duplicate_table,结构和 duplicate_table 一样
mysql> create table duplicate_table_backup like duplicate_table;
Query OK, 0 rows affected (0.02 sec)-- 将 duplicate_table 的去重数据插入到 no_duplicate_table
mysql> insert into duplicate_table_backup select distinct * from duplicate_table;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0-- 通过重命名表,实现原子的去重操作
mysql> alter table duplicate_table rename duplicate_table_old;
Query OK, 0 rows affected (0.01 sec)mysql> alter table duplicate_table_backup rename duplicate_table;
Query OK, 0 rows affected (0.00 sec)-- 查看最终结果
mysql> select * from duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
3 rows in set (0.00 sec)
六、聚合函数
函数 | 说明 |
COUNT([DISTINCT] expr) | 返回查询到的数据的 数量 |
SUM([DISTINCT] expr) | 返回查询到的数据的 总和,不是数字没有意义 |
AVG([DISTINCT] expr) | 返回查询到的数据的 平均值,不是数字没有意义 |
MAX([DISTINCT] expr) | 返回查询到的数据的 最大值,不是数字没有意义 |
MIN([DISTINCT] expr) | 返回查询到的数据的 最小值,不是数字没有意义 |
案例:
1. 统计班级共有多少同学
-- 使用 * 做统计,不受 NULL 影响
mysql> select count(*) 总人数 from exam_result;
+-----------+
| 总人数 |
+-----------+
| 7 |
+-----------+
1 row in set (0.00 sec)-- 使用表达式做统计
mysql> select count(1) 总人数 from exam_result;
+-----------+
| 总人数 |
+-----------+
| 7 |
+-----------+
1 row in set (0.00 sec)
2. 统计班级收集的 qq 号有多少
mysql> select * from students;
+----+----+-----------+------------+
| id | sn | name | qq |
+----+----+-----------+------------+
| 1 | 61 | 路飞 | 111@qq.com |
| 2 | 62 | 索隆 | NULL |
| 4 | 63 | 娜美 | 333@qq.com |
| 5 | 64 | 乔巴 | NULL |
| 6 | 65 | 弗兰奇 | 444@qq.com |
| 7 | 66 | 罗宾 | 555@qq.com |
| 8 | 67 | 布鲁克 | 123@qq.com |
| 9 | 68 | 山治 | |
+----+----+-----------+------------+
8 rows in set (0.00 sec)-- NULL 不会计入结果
mysql> select count(qq) from students;
+-----------+
| count(qq) |
+-----------+
| 6 |
+-----------+
1 row in set (0.00 sec)
3.统计本次考试的数学成绩分数个数
-- COUNT(math) 统计的是全部成绩
mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
| 7 |
+-------------+
1 row in set (0.00 sec)-- count(distinct math)统计的是去重后的成绩
mysql> select count(distinct math) from exam_result;
+----------------------+
| count(distinct math) |
+----------------------+
| 6 |
+----------------------+
1 row in set (0.00 sec)
4.统计数学成绩总分
mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
| 581 |
+-----------+
1 row in set (0.00 sec)-- 不及格 < 60 的总分,没有结果,返回 NULL
mysql> select sum(math) from exam_result where math<60;
+-----------+
| sum(math) |
+-----------+
| NULL |
+-----------+
1 row in set (0.00 sec)
5.统计平均总分
mysql> SELECT AVG(chinese + math + english) 平均总分 FROM exam_result;
+--------------------+
| 平均总分 |
+--------------------+
| 221.14285714285714 |
+--------------------+
1 row in set (0.00 sec)
6.返回英语最高分
mysql> select max(english) from exam_result;
+--------------+
| max(english) |
+--------------+
| 90 |
+--------------+
1 row in set (0.00 sec)
7.返回 > 70 分以上的数学最低分
mysql> select min(math) from exam_result where math>70;
+-----------+
| min(math) |
+-----------+
| 73 |
+-----------+
1 row in set (0.00 sec)
聚合函数:聚合统计一定是直接或者间接统计列方向的某些数据.–>一定是相同属性的!
七、group by子句的使用
在select中使用group by子句可以对指定列进行分组查询
select column1, column2, .. from table group by column;
我们以前select的时候,都是将数据作为一个整体.其实mysql可以支持按照指定的列进行对数据做分组,我们可以让特定的sql在特定的组内进行某种操作!
案例:
- 准备工作,创建一个雇员信息表(来自oracle 9i的经典测试表)
- EMP员工表
- DEPT部门表
- SALGRADE工资等级表
oracle 9i的经典测试表
1.查看表结构
mysql> source scott_data.sql;mysql> use scott;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_scott |
+-----------------+
| dept |
| emp |
| salgrade |
+-----------------+
3 rows in set (0.00 sec)mysql> desc dept;
+--------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------------+------+-----+---------+-------+
| deptno | int(2) unsigned zerofill | NO | | NULL | |
| dname | varchar(14) | YES | | NULL | |
| loc | varchar(13) | YES | | NULL | |
+--------+--------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql> desc emp;
+----------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------------------+------+-----+---------+-------+
| empno | int(6) unsigned zerofill | NO | | NULL | |
| ename | varchar(10) | YES | | NULL | |
| job | varchar(9) | YES | | NULL | |
| mgr | int(4) unsigned zerofill | YES | | NULL | |
| hiredate | datetime | YES | | NULL | |
| sal | decimal(7,2) | YES | | NULL | |
| comm | decimal(7,2) | YES | | NULL | |
| deptno | int(2) unsigned zerofill | YES | | NULL | |
+----------+--------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)mysql> desc salgrade;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| grade | int(11) | YES | | NULL | |
| losal | int(11) | YES | | NULL | |
| hisal | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
2. 如何显示每个部门的平均工资和最高工资
mysql> select deptno,avg(sal),max(sal) from emp group by deptno;
+--------+-------------+----------+
| deptno | avg(sal) | max(sal) |
+--------+-------------+----------+
| 10 | 2916.666667 | 5000.00 |
| 20 | 2175.000000 | 3000.00 |
| 30 | 1566.666667 | 2850.00 |
+--------+-------------+----------+
3 rows in set (0.00 sec)
3. 显示每个部门的每种岗位的平均工资和最低工资
mysql> select deptno,job,avg(sal),min(sal) from emp group by deptno,job;
+--------+-----------+-------------+----------+
| deptno | job | avg(sal) | min(sal) |
+--------+-----------+-------------+----------+
| 10 | CLERK | 1300.000000 | 1300.00 |
| 10 | MANAGER | 2450.000000 | 2450.00 |
| 10 | PRESIDENT | 5000.000000 | 5000.00 |
| 20 | ANALYST | 3000.000000 | 3000.00 |
| 20 | CLERK | 950.000000 | 800.00 |
| 20 | MANAGER | 2975.000000 | 2975.00 |
| 30 | CLERK | 950.000000 | 950.00 |
| 30 | MANAGER | 2850.000000 | 2850.00 |
| 30 | SALESMAN | 1400.000000 | 1250.00 |
+--------+-----------+-------------+----------+
9 rows in set (0.00 sec)
4.显示平均工资低于2000的部门和它的平均工资
- 统计各个部门的平均工资
mysql> select deptno,avg(sal) from emp group by deptno;
+--------+-------------+
| deptno | avg(sal) |
+--------+-------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
+--------+-------------+
3 rows in set (0.00 sec)
- having和group by配合使用,对group by结果进行过滤
mysql> select deptno,avg(sal) from emp group by deptno having avg(sal)<2000;
+--------+-------------+
| deptno | avg(sal) |
+--------+-------------+
| 30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)
--having经常和group by搭配使用,作用是对分组进行筛选,作用有些像where。mysql> select deptno,avg(sal) from emp where 1=1 group by deptno having avg(sal)<2000;
+--------+-------------+
| deptno | avg(sal) |
+--------+-------------+
| 30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)
# 下面这个例子证明了where和having是可以共存的.mysql> select deptno,avg(sal) from emp where sal>1000 group by deptno having avg(sal)<2000;
+--------+-------------+
| deptno | avg(sal) |
+--------+-------------+
| 30 | 1690.000000 |
+--------+-------------+
1 row in set (0.00 sec)
注意:
- group by是通过分组这样的手段,为未来进行聚合统计提供基本的功能支持(group一定是配合聚合统计使用的);
- group by 后面跟的都是分组的字段依据,只有在group by后面出现的字段,未来在聚合统计的时候,在select中才能出现.
- where VS having 不是冲突的,是互相补充的,having通常是在完成整个分组聚合统计,然后在进行筛选;where通常是在表中数据初步被筛选的时候起效果的.
总结
SQL查询中各个关键字的执行先后顺序 from > on> join > where > group by > with > having > select
> distinct > order by > limit;
(本章完!)