07 - MySQL表的增删查改
07 - MySQL表的增删查改
SarznCRUD
Create创建 Retrieve读取 Update更新 Delete删除
insert
1 | INSERT [INTO] table_name [(column, [, column]...)] VALUES (value_list) [, (value_list)]... |
单行插入 1
2
3
4
5
6
7
8
9
10mysql> insert into qq_user (sn, name, qq) values (123, 'a', '4567890');
Query OK, 1 row affected (0.00 sec)
mysql> select * from qq_user;
+----+-----+------+---------+
| id | sn | name | qq |
+----+-----+------+---------+
| 1 | 123 | a | 4567890 |
+----+-----+------+---------+
1 row in set (0.00 sec)
多行插入 1
2
3
4
5
6
7
8
9
10
11
12
13
14mysql> insert into qq_user (sn, name, qq) values (234, 'b', '235456'), (435, 'c', '678942'), (294, 'd', '257283');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from qq_user;
+----+-----+------+---------+
| id | sn | name | qq |
+----+-----+------+---------+
| 1 | 123 | a | 4567890 |
| 2 | 234 | b | 235456 |
| 3 | 435 | c | 678942 |
| 4 | 294 | d | 257283 |
+----+-----+------+---------+
4 rows in set (0.00 sec)
插入替换
可以选择性的进行同步更新的语法
1 | INSERT ... ON DUPLICATE KEY UPDATE column = value [, column = value] ... |
使用 on duplicate key update 便支持在冲突发生的时候执行后面这个 update 语句, 如果没有冲突, 直接执行的就是前面的这个 insert into 语句 注意书写的时候不要漏了 on duplicate key 的这个 key, 以及 update 多列之后的这个逗号隔开
1 | mysql> insert into qq_user (sn, name, qq) values (123, 'change_a', '232323') on duplicate key update name='change_a_after_dup', qq='111111'; |
使用 row_count 函数查询上一条语句受影响的行数, 如果是-1
则代表上一条是 select, show 这样的查询语句 1
2
3
4
5
6
7mysql> select row_count();
+-------------+
| row_count() |
+-------------+
| -1 |
+-------------+
1 row in set (0.00 sec)
替换
1 | REPLACE INTO table_name (sn, name) VALUES (20001, 'replace_b'); |
如果出现:
- 1 row affected 代表没有冲突的列, 直接插入新的列
- 2 row affected 代表有冲突的列, 将冲突的列删掉了, 再插入了新的列
1 | mysql> replace into qq_user(sn, name, qq) values (658, 'e', '239829'); |
查询
1 | SELECT |
我们建立一张这样的表 1
2
3
4
5
6
7mysql> create table exam_result( id int unsigned primary key auto_increment, name varchar(20) not null, chinese float default 0.0, math floa
t default 0.0, english float default 0.0 );
Query OK, 0 rows affected (0.02 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 | mysql> select * from exam_result; |
指定列查询
1 | mysql> select math, name from exam_result; |
查询字段为表达式
select 后面就是我们要执行的表达式, 它同时也可以是一种数学运算
1 | mysql> select id, name, chinese+math+english from exam_result; |
与此同时, 我们还可以使用 as 对字段进行重命名
1 | mysql> select id as student_number, name as student_name, chinese+math+english as student_total from exam_result; |
这个 as 也可以省略
1 | mysql> select id student_number, name student_name, chinese+math+english student_total from exam_result; |
查询去重
使用 select distinct 可以对查询结果进行去重
1 | mysql> select distinct math from exam_result; |
where 子句
where 影响的是我们最终拿到结果的条目数, 也就是行数 而 select 后面接的筛选的是显示的列数
比较运算符
逻辑运算符
where 案例
– 使用 where 筛选分数低于 60 分的人 1
2
3
4
5
6
7
8
9mysql> select name, english from exam_result where english<60;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56 |
| 刘玄德 | 45 |
| 宋公明 | 30 |
+-----------+---------+
3 rows in set (0.00 sec)
– 筛选出语文成绩在 [80,90] 分的同学, 和他的语文成绩
1
2
3
4
5
6
7
8
9mysql> select name, chinese from exam_result where chinese between 80 and 90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孙悟空 | 87 |
| 猪悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)
也可以使用 and 连接比较运算符进行筛选 1
2
3
4
5
6
7
8
9mysql> select name, chinese from exam_result where chinese >= 80 and chinese <= 90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孙悟空 | 87 |
| 猪悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)
– 数学成绩是 58 或者 59 或者 98 或者 99 分的同学及数学成绩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17mysql> select name, math from exam_result where math in(58, 59, 98, 99);
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 猪悟能 | 98 |
+-----------+------+
2 rows in set (0.00 sec)
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)
– 姓孙的同学 1
2
3
4
5
6
7
8mysql> select name from exam_result where name like '孙%';
+-----------+
| name |
+-----------+
| 孙悟空 |
| 孙权 |
+-----------+
2 rows in set (0.00 sec)
– 孙某同学 1
2
3
4
5
6
7mysql> select name from exam_result where name like '孙_';
+--------+
| name |
+--------+
| 孙权 |
+--------+
1 row in set (0.00 sec)
– 语文成绩好于英语成绩的同学 1
2
3
4
5
6
7
8
9
10
11mysql> 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)
– 总分在 200 以下的同学 1
2mysql> select name, chinese+math+english total from exam_result where total < 200;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
如果这样写的话你会发现报错了. sql 语句的执行顺序非常关键 因为在这个 sql 中, 我们会先执行 from 再执行 where 最后是 select
我们不能在 where 中做重命名, 语法不支持 1
2mysql> select name, total from exam_result where chinese+math+english as total < 200;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as total < 200' at line 1
那么我们只能写成 1
2
3
4
5
6
7
8mysql> select name, chinese + math + english as total from exam_result where chinese + math + english < 200;
+-----------+-------+
| name | total |
+-----------+-------+
| 刘玄德 | 185 |
| 宋公明 | 170 |
+-----------+-------+
2 rows in set (0.00 sec)
– 语文成绩>80 并且 不姓孙的同学 1
2
3
4
5
6
7
8mysql> select chinese, name from exam_result where chinese > 80 and name not like '孙%';
+---------+-----------+
| chinese | name |
+---------+-----------+
| 88 | 猪悟能 |
| 82 | 曹孟德 |
+---------+-----------+
2 rows in set (0.00 sec)
– 孙某同学, 否则要求 总成绩>200 并且
语文成绩<数学成绩 并且 英语成绩>80 1
2
3
4
5
6
7
8
9mysql> select name, chinese + math + english as total, chinese, math, english from exam_result where (name like '孙_') or (chinese + math +
english > 200 and chinese < math and english > 80);
+-----------+-------+---------+------+---------+
| name | total | chinese | math | english |
+-----------+-------+---------+------+---------+
| 猪悟能 | 276 | 88 | 98 | 90 |
| 孙权 | 221 | 70 | 73 | 78 |
+-----------+-------+---------+------+---------+
2 rows in set (0.00 sec)
null 相关
建立一张这样的表 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32mysql> create table t9(
-> id int,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t9 values(1, 'Sarzn');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t9 values(null, 'Sarzn');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t9 values(1, null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t9 values(null, null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t9 values(1, '');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t9;
+------+-------+
| id | name |
+------+-------+
| 1 | Sarzn |
| NULL | Sarzn |
| 1 | NULL |
| NULL | NULL |
| 1 | |
+------+-------+
5 rows in set (0.01 sec)
– 名字是 null 的同学 1
2
3
4
5
6
7
8mysql> select * from t9 where name is null;
+------+------+
| id | name |
+------+------+
| 1 | NULL |
| NULL | NULL |
+------+------+
2 rows in set (0.00 sec)
order by 子句
1 | -- ASC 为升序 ascending order |
注意! 没有 order by 子句的查询, 返回的顺序是未定义的, 永远不要依赖这个顺序
order by 案例
– 同学及数学成绩, 按照数学成绩升序显示 1
2
3
4
5
6
7
8
9
10
11
12
13mysql> 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)
– 查询同学各门成绩, 依次按照 数学降序, 英语降序, 语文升序的方式显示
1
2
3
4
5
6
7
8
9
10
11
12
13mysql> select name, math, english, chinese from exam_result order by math desc, english desc, chinese asc;
+-----------+------+---------+---------+
| name | math | english | chinese |
+-----------+------+---------+---------+
| 猪悟能 | 98 | 90 | 88 |
| 唐三藏 | 98 | 56 | 67 |
| 刘玄德 | 85 | 45 | 55 |
| 曹孟德 | 84 | 67 | 82 |
| 孙悟空 | 78 | 77 | 87 |
| 孙权 | 73 | 78 | 70 |
| 宋公明 | 65 | 30 | 75 |
+-----------+------+---------+---------+
7 rows in set (0.00 sec)
– 查询同学及总分, 总分从高到低 1
2
3
4
5
6
7
8
9
10
11
12
13mysql> select name, chinese + math + english as total from exam_result order by total asc;
+-----------+-------+
| name | total |
+-----------+-------+
| 宋公明 | 170 |
| 刘玄德 | 185 |
| 唐三藏 | 221 |
| 孙权 | 221 |
| 曹孟德 | 233 |
| 孙悟空 | 242 |
| 猪悟能 | 276 |
+-----------+-------+
7 rows in set (0.00 sec)
这里的执行先后顺序是: 先执行 from -> where -> select -> order by 所以我们可以直接使用 total 这个别名了
– 查询()姓孙的同学或者姓曹的同学)的数学成绩,
结果按照数学成绩从低到高显示 1
2
3
4
5
6
7
8
9mysql> select name, math from exam_result where name like '孙%' or name like '曹%' order by math asc;
+-----------+------+
| name | math |
+-----------+------+
| 孙权 | 73 |
| 孙悟空 | 78 |
| 曹孟德 | 84 |
+-----------+------+
3 rows in set (0.00 sec)
筛选分页结果
1 | -- 起始下标为 0 |
建议: 对未知表进行查询的时候, 最好加一条 LIMIT 1, 避免因为表中数据过大, 查询全表数据导致数据库卡死 这里的执行先后顺序是: 先执行 from -> where -> select -> order by -> limit
– 显示表中的前 3 行 1
2
3
4
5
6
7
8
9mysql> select * from exam_result limit 3 offset 0;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
+----+-----------+---------+------+---------+
3 rows in set (0.01 sec)
update
对查询到的结果进行列值更新
1 | UPDATE table_name SET column = expr[, column = expr ...] |
update 案例
– 将孙悟空同学的数学成绩变更为 80 分 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17mysql> update exam_result set math=80 where name='孙悟空';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select name, math from exam_result;
+-----------+------+
| name | math |
+-----------+------+
| 唐三藏 | 98 |
| 孙悟空 | 80 |
| 猪悟能 | 98 |
| 曹孟德 | 84 |
| 刘玄德 | 85 |
| 孙权 | 73 |
| 宋公明 | 65 |
+-----------+------+
7 rows in set (0.00 sec)
– 将曹孟德同学的数学成绩变更为 60 分, 语文成绩变更为 70 分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mysql> update exam_result set math = 60, chinese = 70 where name='曹孟德';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 80 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 70 | 60 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
– 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
1 | mysql> update exam_result set math = math + 30 order by chinese+math+english asc limit 3 offset 0; |
这里报错是因为 MySQL 的 UPDATE 支持 LIMIT,但不支持 OFFSET
1 | mysql> update exam_result set math = math + 30 order by chinese+math+english asc limit 3; |
– 将所有同学的语文成绩变为原来的两倍
注意! 整表的更新要小心
1 | mysql> update exam_result set chinese = chinese * 2; |
1 | mysql> select name, chinese from exam_result; |
delete 删除
1 | DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...] |
delete 案例
– 删除孙悟空同学的考试成绩 1
2mysql> delete from exam_result where name='孙悟空';
Query OK, 1 row affected (0.01 sec)
– 删除总成绩倒数第一的同学 1
2mysql> delete from exam_result order by chinese+math+english limit 1;
Query OK, 1 row affected (0.01 sec)
删除整张表与删除表中的数据
除了 delete from 和 drop table 我们还有一种删除的方式, 叫做截断表
截断表 truncate table
1 | TRUNCATE [TABLE] table_name |
注意这个操作慎用!
- 只能对整表进行操作, 不能像 delete from 一样对部分的数据进行操作
- 实际上 mysql 不对数据操作, 所有删除的比 delete from 更快, 但是 truncate table 在删除的时候并不经过真正的事物, 所以无法回滚
- 会重置 AUTO_INCREMENT 项
使用 truncate table 删除
1 | mysql> truncate table for_truncate; |
delete from 是删除表中的数据, 但是表结构不变 truncate table 是整表的数据和表结构都会删掉, 但是表还在 drop table 真的就是把整张表删掉了
– 创建一张这样的表 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16ysql> create table for_delete( id int primary key auto_increment, name varchar(20) );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into for_delete (name) values ('A'), ('B'), ('C');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
– 删除表中的所有数据 1
2
3
4
5mysql> delete from for_delete;
Query OK, 3 rows affected (0.00 sec)
mysql> select * from for_delete;
Empty set (0.00 sec)
表中的所有数据被删除之后我们再查看 AUTO_INCREMENT 这个属性值,
发现这个属性值并没有被清空 1
2
3
4
5
6
7
8
9mysql> show create table for_delete\G
*************************** 1. row ***************************
Table: for_delete
Create Table: CREATE TABLE `for_delete` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3
1 row in set (0.00 sec)
所以我们下次再插入的时候会直接自增为 4
插入查询结果
1 | INSERT INTO table_name [(column [, column ...])] SELECT .. |
案例, 删除表中的重复记录
– 创建表 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19mysql> 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
mysql> select * from duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 100 | aaa |
| 200 | bbb |
| 200 | bbb |
| 200 | bbb |
| 300 | ccc |
+------+------+
6 rows in set (0.00 sec)
我们对表中的元素进行去重操作的思路是
- 创建一个空表, 表结构和被去重的表的结构一样
1
2mysql> create table no_duplicate_table like duplicate_table;
Query OK, 0 rows affected (0.02 sec) - 将 duplicate_table 查询去重的结果插入到 no_duplicate_table 中
1
2
3
4
5
6
7
8
9
10
11
12
13mysql> insert into no_duplicate_table select distinct * from duplicate_table;
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from no_duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
3 rows in set (0.00 sec) - 使用 rename table 将旧表重命名为其他名字, 将新表重命名为旧表的名字
1
2
3
4
5
6
7
8
9
10
11
12mysql> rename table duplicate_table to old_duplicate_table, no_duplicate_table to duplicate_table;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
3 rows in set (0.00 sec)
为什么最后是通过 rename 的方式进行的? 就是单纯的想要等一切都就绪了, 然后再统一放入, 更新, 生效. 这与冗长的插入, 上传相比是十分的简洁. 同一时刻你的这个旧数据库可能被其他网站, 其他程序在访问, 我们直接新建一个表传完, 插入完数据后再统一进行 mv 的操作是比较好的 这也使得我们去重行为是原子的
聚合函数
| 函数 | 说明 |
|---|---|
| COUNT ([DISTINCT] expr) | 返回查询到的数据的 数量 |
| SUM ([DISTINCT] expr) | 返回查询到的数据的 总和, 不是数字没有意义 |
| AVG ([DISTINCT] expr) | 返回查询到的数据的 平均值, 不是数字没有意义 |
| MAX ([DISTINCT] expr) | 返回查询到的数据的 最大值, 不是数字没有意义 |
| MIN ([DISTINCT] expr) | 返回查询到的数据的 最小值, 不是数字没有意义 |
我们有下表 1
2
3
4
5
6
7
8
9
10
11mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 134 | 98 | 56 |
| 3 | 猪悟能 | 176 | 98 | 90 |
| 4 | 曹孟德 | 140 | 90 | 67 |
| 6 | 孙权 | 140 | 73 | 78 |
| 7 | 宋公明 | 150 | 95 | 30 |
+----+-----------+---------+------+---------+
5 rows in set (0.00 sec)
count 统计
– 统计班级有多少同学 使用 count()
1 | mysql> select count(*) from exam_result; |
– 统计 math 列中有多少行 1
2
3
4
5
6
7mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
| 5 |
+-------------+
1 row in set (0.00 sec)
– 统计 math 列中有多少行不同的成绩 使用 distinct math 进行去重
1
2
3
4
5
6
7mysql> select count(distinct math) from exam_result;
+----------------------+
| count(distinct math) |
+----------------------+
| 4 |
+----------------------+
1 row in set (0.00 sec)
sum 统计
– 统计数学的总分是多少 1
2
3
4
5
6
7mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
| 454 |
+-----------+
1 row in set (0.00 sec)
– 计算班级的数学平均分是多少 1
2
3
4
5
6
7mysql> select sum(math)/count(math) from exam_result;
+-----------------------+
| sum(math)/count(math) |
+-----------------------+
| 90.8 |
+-----------------------+
1 row in set (0.00 sec)
avg 计算平均值
– 计算班级的数学平均分是多少 1
2
3
4
5
6
7mysql> select avg(math) from exam_result;
+-----------+
| avg(math) |
+-----------+
| 90.8 |
+-----------+
1 row in set (0.00 sec)
– 统计总成绩的平均分 1
2
3
4
5
6
7mysql> select avg(chinese+math+english) from exam_result;
+---------------------------+
| avg(chinese+math+english) |
+---------------------------+
| 303 |
+---------------------------+
1 row in set (0.00 sec)
max 取当列的最大值
– 找到数学成绩的最大值 1
2
3
4
5
6
7mysql> select max(math) from exam_result;
+-----------+
| max(math) |
+-----------+
| 98 |
+-----------+
1 row in set (0.00 sec)
分组聚合
先分组再聚合
分组的目的是为了进行分组之后, 方便进行聚合统计 在 select 中使用 group by 子句可以对指定列进行分组查询
1
select column1, colunm2, ... from table group by column;
这里使用 scott_data.sql 进行演示(oracle 9i的经典测试表)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35mysql> 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> select * from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)
1 | mysql> desc dept; |
1 | mysql> desc salgrade; |
- 一般来说只有在 group by 中出现了的列名才能在前面的 select 中出现, 除非是聚合函数. 因为只有分了组, 或者使用聚合函数才能进行聚合统计
- group by 指定列名, 实际分组是用该列的不同的行数据来进行分组的
- 分组(‘分表’), 不就是把一长大表按照条件在逻辑上拆成了多个子表, 然后分别对各自的子表进行聚合统计
– 显示每个部门的平均工资和最高工资 1
2
3
4
5
6
7
8
9mysql> select deptno, max(sal), avg(sal) from emp group by deptno;
+--------+----------+-------------+
| deptno | max(sal) | avg(sal) |
+--------+----------+-------------+
| 20 | 3000.00 | 2175.000000 |
| 30 | 2850.00 | 1566.666667 |
| 10 | 5000.00 | 2916.666667 |
+--------+----------+-------------+
3 rows in set (0.00 sec)
– 显示每个部门的每种岗位的平均工资和最高工资 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15mysql> select deptno, job, avg(sal), max(sal) from emp group by deptno, job order by deptno, avg(sal) desc;
+--------+-----------+-------------+----------+
| deptno | job | avg(sal) | max(sal) |
+--------+-----------+-------------+----------+
| 10 | PRESIDENT | 5000.000000 | 5000.00 |
| 10 | MANAGER | 2450.000000 | 2450.00 |
| 10 | CLERK | 1300.000000 | 1300.00 |
| 20 | ANALYST | 3000.000000 | 3000.00 |
| 20 | MANAGER | 2975.000000 | 2975.00 |
| 20 | CLERK | 950.000000 | 1100.00 |
| 30 | MANAGER | 2850.000000 | 2850.00 |
| 30 | SALESMAN | 1400.000000 | 1600.00 |
| 30 | CLERK | 950.000000 | 950.00 |
+--------+-----------+-------------+----------+
9 rows in set (0.00 sec)
– 显示平均工资低于 2000 的部门和他的平均工资
意思是:按部门分组,计算每个部门的平均工资,只保留平均工资低于 2000 的部门。
如果使用了 where 是错误的. 因为 where 做的事情是把所有的
平均工资<2000 的员工筛选出来 1
2mysql> select deptno, avg(sal) from emp where avg(sal)<2000 group by deptno;
ERROR 1111 (HY000): Invalid use of group function
正确的做法是使用 having 1
2
3
4
5
6
7mysql> 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 是对聚合后的统计数据, 再进行条件筛选
WHERE sal < 2000:先筛选工资低于 2000 的员工,再参与分组计算。HAVING AVG(sal) < 2000:先计算部门平均工资,再筛选平均工资低于 2000 的部门。
所以执行的顺序是 from -> where -> group by -> select -> having





