目录
一,create(创建表)
(1)创建表。
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(64) unique key
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc students;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| sn | int(10) unsigned | NO | UNI | NULL | |
| name | varchar(64) | NO | | NULL | |
| qq | varchar(64) | YES | UNI | NULL | |
+-------+------------------+------+-----+---------+----------------+
(2)单行数据+全列插入。
mysql> insert into students (sn, name, qq) values(123, '张飞', '123@qq.com');
Query OK, 1 row affected (0.00 sec)
mysql> insert into students(sn, name) values(456, '刘备');
Query OK, 1 row affected (0.01 sec)
mysql> insert into students(id, sn, name, qq) values(100, 666,'唐三藏', '666@qq.com');
Query OK, 1 row affected (0.01 sec)
mysql> insert into students(id, sn, name, qq) values(101, 777,'孙悟空', '777@qq.com');
Query OK, 1 row affected (0.00 sec)
mysql> select*from students;
+-----+-----+-----------+------------+
| id | sn | name | qq |
+-----+-----+-----------+------------+
| 1 | 123 | 张飞 | 123@qq.com |
| 2 | 456 | 刘备 | NULL |
| 100 | 666 | 唐三藏 | 666@qq.com |
| 101 | 777 | 孙悟空 | 777@qq.com |
+-----+-----+-----------+------------+
(3)多行数据+指定列插入。
mysql> insert into students(sn, name) values(789, '关羽'),(100,'孙权');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select*from students;
+-----+-----+-----------+------------+
| id | sn | name | qq |
+-----+-----+-----------+------------+
| 1 | 123 | 张飞 | 123@qq.com |
| 2 | 456 | 刘备 | NULL |
| 3 | 789 | 关羽 | NULL |
| 4 | 100 | 孙权 | NULL |
| 100 | 666 | 唐三藏 | 666@qq.com |
| 101 | 777 | 孙悟空 | 777@qq.com |
+-----+-----+-----------+------------+
(4)插入否则更新
主键冲突,唯一键冲突,因为对应的值已经存在,都会导致插入失败。
mysql> desc students;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| sn | int(10) unsigned | NO | UNI | NULL | |
| name | varchar(64) | NO | | NULL | |
| qq | varchar(64) | YES | UNI | NULL | |
+-------+------------------+------+-----+---------+----------------+
mysql> select*from students;
+-----+-----+-----------+------------+
| id | sn | name | qq |
+-----+-----+-----------+------------+
| 1 | 123 | 张飞 | 123@qq.com |
| 2 | 456 | 刘备 | NULL |
| 3 | 789 | 关羽 | NULL |
| 4 | 100 | 孙权 | NULL |
| 100 | 666 | 唐三藏 | 666@qq.com |
| 101 | 777 | 孙悟空 | 777@qq.com |
+-----+-----+-----------+------------+
mysql> insert into students (id, sn, name) values (4, 100,'周瑜');
ERROR 1062 (23000): Duplicate entry '4' for key 'PRIMARY'
mysql> insert into students (sn, name) values (100,'周瑜');
ERROR 1062 (23000): Duplicate entry '100' for key 'sn'
这时候可以使用,选择性进行同步更新操作,语法:
INSERT INTO 表名 (列1, 列2, 列3) VALUES (值1,值2,值3) ON DUPLICATE KEY UPDATE 列1 = 值1 , 列2 = 值2 ...
mysql> insert into students (id, sn, name) values (4, 100,'周瑜')
on duplicate key update sn=100, name='周瑜';
Query OK, 2 rows affected (0.00 sec)
-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新
mysql> select*from students;
+-----+-----+-----------+------------+
| id | sn | name | qq |
+-----+-----+-----------+------------+
| 1 | 123 | 张飞 | 123@qq.com |
| 2 | 456 | 刘备 | NULL |
| 3 | 789 | 关羽 | NULL |
| 4 | 100 | 周瑜 | NULL |
| 100 | 666 | 唐三藏 | 666@qq.com |
| 101 | 777 | 孙悟空 | 777@qq.com |
+-----+-----+-----------+------------+
(5)替换
mysql> replace into students (sn,name) values (100,'哈哈');
Query OK, 2 rows affected (0.00 sec)
mysql> select * from students;
+-----+-----+-----------+------------+
| id | sn | name | qq |
+-----+-----+-----------+------------+
| 1 | 123 | 张飞 | 123@qq.com |
| 2 | 456 | 刘备 | NULL |
| 3 | 789 | 关羽 | NULL |
| 100 | 666 | 唐三藏 | 666@qq.com |
| 101 | 777 | 孙悟空 | 777@qq.com |
| 109 | 100 | 哈哈 | NULL |
+-----+-----+-----------+------------+
6 rows in set (0.00 sec)
mysql> replace into students (sn,name) values (200,'嘿嘿');
Query OK, 1 row affected (0.01 sec)
mysql> select * from students;
+-----+-----+-----------+------------+
| id | sn | name | qq |
+-----+-----+-----------+------------+
| 1 | 123 | 张飞 | 123@qq.com |
| 2 | 456 | 刘备 | NULL |
| 3 | 789 | 关羽 | NULL |
| 100 | 666 | 唐三藏 | 666@qq.com |
| 101 | 777 | 孙悟空 | 777@qq.com |
| 109 | 100 | 哈哈 | NULL |
| 110 | 200 | 嘿嘿 | NULL |
+-----+-----+-----------+------------+
7 rows in set (0.00 sec)
二,retrieve(查询表)
1,select列
(1)创建表
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 score (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)
Records: 7 Duplicates: 0 Warnings: 0
(2)全列查询
mysql> select * from score;
+----+--------+---------+------+---------+
| 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)
(3)指定列查询
mysql> select id,name,english from score;
+----+--------+---------+
| id | name | english |
+----+--------+---------+
| 1 | 唐一 | 56 |
| 2 | 孙二 | 77 |
| 3 | 张三 | 90 |
| 4 | 李四 | 67 |
| 5 | 王五 | 45 |
| 6 | 赵六 | 78 |
| 7 | 田七 | 30 |
+----+--------+---------+
7 rows in set (0.00 sec)
(4)查询字段为表达式
mysql> select id,name, chinese+math+english from score;
+----+--------+---------+------+---------+
| 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 |
+----+--------+---------+------+---------+
(5)为查询结果指定别名
mysql> select id,name, chinese+math+english as total from score;
+----+--------+-------+
| id | name | total |
+----+--------+-------+
| 1 | 唐一 | 221 |
| 2 | 孙二 | 242 |
| 3 | 张三 | 276 |
| 4 | 李四 | 233 |
| 5 | 王五 | 185 |
| 6 | 赵六 | 221 |
| 7 | 田七 | 170 |
+----+--------+-------+
7 rows in set (0.00 sec)
(6)结果去重,数学98分重复
mysql> select distinct math from score;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
mysql> select math from score;
+------+
| math |
+------+
| 98 |
| 78 |
| 98 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
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) |
(1)条件筛选
mysql> select * from score;
+----+--------+---------+------+---------+
| 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)
mysql> select name english from score where english < 60;
+---------+
| english |
+---------+
| 唐一 |
| 王五 |
| 田七 |
+---------+
3 rows in set (0.00 sec)
mysql> select name,chinese,english from score where chinese >= 80 and english <= 90;
+--------+---------+---------+
| name | chinese | english |
+--------+---------+---------+
| 孙二 | 87 | 77 |
| 张三 | 88 | 90 |
| 李四 | 82 | 67 |
+--------+---------+---------+
3 rows in set (0.00 sec)
mysql> select name,chinese from score where chinese between 60 and 80;
+--------+---------+
| name | chinese |
+--------+---------+
| 唐一 | 67 |
| 赵六 | 70 |
| 田七 | 75 |
+--------+---------+
3 rows in set (0.00 sec)
mysql> select name,math from score where math=58 or math=59 or math=98 or math=99;
+--------+------+
| name | math |
+--------+------+
| 唐一 | 98 |
| 张三 | 98 |
+--------+------+
2 rows in set (0.00 sec)
-- % 匹配任意多个(包括 0 个)任意字符
mysql> select name from score where name like '孙%';
+--------+
| name |
+--------+
| 孙二 |
+--------+
1 row in set (0.00 sec)
-- _ 匹配严格的一个任意字符
mysql> select name from score where name like '孙_';
+--------+
| name |
+--------+
| 孙二 |
+--------+
1 row in set (0.00 sec)
mysql> select name,chinese from score where chinese between chinese and english;
+--------+---------+
| name | chinese |
+--------+---------+
| 张三 | 88 |
| 赵六 | 70 |
+--------+---------+
2 rows in set (0.00 sec)
mysql> select name,chinese from score where chinese > english;
+--------+---------+
| name | chinese |
+--------+---------+
| 唐一 | 67 |
| 孙二 | 87 |
| 李四 | 82 |
| 王五 | 55 |
| 田七 | 75 |
+--------+---------+
5 rows in set (0.00 sec)
mysql> select name,chinese+math+english from score where chinese+math+english < 200;
+--------+----------------------+
| name | chinese+math+english |
+--------+----------------------+
| 王五 | 185 |
| 田七 | 170 |
+--------+----------------------+
2 rows in set (0.00 sec)
mysql> select name,chinese+math+english from score where chinese+math+english > 200 and name not like '孙%';
+--------+----------------------+
| name | chinese+math+english |
+--------+----------------------+
| 唐一 | 221 |
| 张三 | 276 |
| 李四 | 233 |
| 赵六 | 221 |
+--------+----------------------+
4 rows in set (0.00 sec)
mysql> select name,chinese,math,english from score where name like '孙_' or (chinese+math+english > 200 and chinese < math and english > 80);
+--------+---------+------+---------+
| name | chinese | math | english |
+--------+---------+------+---------+
| 孙二 | 87 | 78 | 77 |
| 张三 | 88 | 98 | 90 |
+--------+---------+------+---------+
2 rows in set (0.00 sec)
mysql> select name qq from students where qq is not null;
+-----------+
| qq |
+-----------+
| 张飞 |
| 唐三藏 |
| 孙悟空 |
+-----------+
3 rows in set (0.00 sec)
mysql> select null = null, null = 1, null = 0, null <=> null, null <=> 1, null <=>0;
+-------------+----------+----------+---------------+------------+-----------+
| null = null | null = 1 | null = 0 | null <=> null | null <=> 1 | null <=>0 |
+-------------+----------+----------+---------------+------------+-----------+
| NULL | NULL | NULL | 1 | 0 | 0 |
+-------------+----------+----------+---------------+------------+-----------+
1 row in set (0.00 sec)
mysql> select name,math,english from score order by math desc;
+--------+------+---------+
| name | math | english |
+--------+------+---------+
| 唐一 | 98 | 56 |
| 张三 | 98 | 90 |
| 王五 | 85 | 45 |
| 李四 | 84 | 67 |
| 孙二 | 78 | 77 |
| 赵六 | 73 | 78 |
| 田七 | 65 | 30 |
+--------+------+---------+
7 rows in set (0.00 sec)
(2)结果排序
mysql> select name,math,english from score order by math desc, english asc;
+--------+------+---------+
| name | math | english |
+--------+------+---------+
| 唐一 | 98 | 56 |
| 张三 | 98 | 90 |
| 王五 | 85 | 45 |
| 李四 | 84 | 67 |
| 孙二 | 78 | 77 |
| 赵六 | 73 | 78 |
| 田七 | 65 | 30 |
+--------+------+---------+
7 rows in set (0.00 sec)
mysql> select name,math,english from score order by math asc, english asc;
+--------+------+---------+
| name | math | english |
+--------+------+---------+
| 田七 | 65 | 30 |
| 赵六 | 73 | 78 |
| 孙二 | 78 | 77 |
| 李四 | 84 | 67 |
| 王五 | 85 | 45 |
| 唐一 | 98 | 56 |
| 张三 | 98 | 90 |
+--------+------+---------+
7 rows in set (0.00 sec)
mysql> select name,math from score where name like '孙%' or name like '李%' order by math desc;
+--------+------+
| name | math |
+--------+------+
| 李四 | 84 |
| 孙二 | 78 |
+--------+------+
2 rows in set (0.01 sec)
mysql> select name,math from score where name like '孙%' or name like '李%' order by math asc;
+--------+------+
| name | math |
+--------+------+
| 孙二 | 78 |
| 李四 | 84 |
+--------+------+
2 rows in set (0.00 sec)
(3)结果分页
mysql> select id,name,math,english,chinese from score 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)
mysql> select id,name,math,english,chinese from score 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)
mysql> select id,name,math,english,chinese from score order by id limit 3 offset 6;
+----+--------+------+---------+---------+
| id | name | math | english | chinese |
+----+--------+------+---------+---------+
| 7 | 田七 | 65 | 30 | 75 |
+----+--------+------+---------+---------+
1 row in set (0.00 sec)
三,update(更新表)
mysql> select name,chinese+math+english as total from score order by chinese+math+english limit 3;
+--------+-------+
| name | total |
+--------+-------+
| 田七 | 170 |
| 王五 | 185 |
| 李四 | 207 |
+--------+-------+
3 rows in set (0.00 sec)
mysql> update score 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
mysql> select name,math,chinese+math+english as total from score order by chinese+math+english limit 3;
+--------+------+-------+
| name | math | total |
+--------+------+-------+
| 田七 | 95 | 200 |
| 王五 | 115 | 215 |
| 唐一 | 98 | 221 |
+--------+------+-------+
3 rows in set (0.00 sec)
mysql> update score set chinese = chinese * 2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0
mysql> select * from score;
+----+--------+---------+------+---------+
| id | name | chinese | math | english |
+----+--------+---------+------+---------+
| 1 | 唐一 | 134 | 98 | 56 |
| 2 | 孙二 | 174 | 78 | 77 |
| 3 | 张三 | 140 | 98 | 90 |
| 4 | 李四 | 130 | 105 | 67 |
| 5 | 王五 | 110 | 115 | 45 |
| 6 | 赵六 | 140 | 73 | 78 |
| 7 | 田七 | 150 | 95 | 30 |
+----+--------+---------+------+---------+
7 rows in set (0.00 sec)
四,delete(删除表)
(1)删除行数据
mysql> select * from score where name = '赵六';
+----+--------+---------+------+---------+
| id | name | chinese | math | english |
+----+--------+---------+------+---------+
| 6 | 赵六 | 140 | 73 | 78 |
+----+--------+---------+------+---------+
1 row in set (0.00 sec)
mysql> delete from score where name = '赵六';
Query OK, 1 row affected (0.00 sec)
mysql> select * from score where name = '赵六';
Empty set (0.00 sec)
(2)删除整张表数据
mysql> CREATE TABLE for_delete (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (0.01 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)
mysql> delete from for_delete;
Query OK, 3 rows affected (0.00 sec)
mysql> select * from for_delete;
Empty set (0.00 sec)
mysql> INSERT INTO for_delete (name) VALUES ('D');
Query OK, 1 row affected (0.00 sec)
mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
| 4 | D |
+----+------+
1 row in set (0.00 sec)
mysql> show create table for_delete \G
*************************** 1. row ***************************
Table: for_delete
Create Table: CREATE TABLE `for_delete` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
(3)截断表
语法:truncate table_name
注意,慎用此操作:
1. 只能对整表操作,不能像 DELETE 一样针对部分数据操作;
2. 实际上 MySQL 不对数据操作,所以比 DELETE 更快,但是TRUNCATE在删除数据的时候,并不经过真正的事物,所以无法回滚
3. 会重置 AUTO_INCREMENT 项
mysql> CREATE TABLE for_truncate (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(20)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into for_truncate (name) values ('A')('B')('C');
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 '('B')('C')' at line 1
mysql> insert into for_truncate (name) values ('A'),('B'),('C');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
mysql> truncate for_truncate;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into for_truncate (name) values ('D');
Query OK, 1 row affected (0.00 sec)
mysql> select * from for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | D |
+----+------+
1 row in set (0.00 sec)
mysql> show create table for_truncate\G
*************************** 1. row ***************************
Table: for_truncate
Create Table: CREATE TABLE `for_truncate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
五,插入查询结果
mysql> create table duplicate_table (id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into duplicate_table values
-> (100,'aaa'),
-> (200,'bbb'),
-> (300,'ccc'),
-> (100,'aaa'),
-> (300,'ccc'),
-> (300,'ccc'),
-> (300,'ccc');
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> create table no_dunplicate_table like duplicate_table;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into no_duplicate_table select distinct * from duplicate_table;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
| 100 | aaa |
| 300 | ccc |
| 300 | ccc |
| 300 | ccc |
+------+------+
7 rows in set (0.01 sec)
mysql> select * from no_duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
3 rows in set (0.00 sec)
mysql> rename table duplicate_table to old_duplicate_table,no_duplicate_table to 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) |
返回查询到的数据的最小值,不是数字没有意义 |
-- 使用 * 做统计,不受 NULL 影响
mysql> select count(*) from students;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
-- 使用表达式做统计
mysql> select count(1) from students;
+----------+
| count(1) |
+----------+
| 7 |
+----------+
1 row in set (0.00 sec)
-- NULL 不会计入结果
mysql> select count(qq) from students;
+-----------+
| count(qq) |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
-- COUNT(math) 统计的是全部成绩
mysql> select count(math) from score;
+-------------+
| count(math) |
+-------------+
| 6 |
+-------------+
1 row in set (0.00 sec)
-- COUNT(DISTINCT math) 统计的是去重成绩数量
mysql> select count(distinct math) from score;
+----------------------+
| count(distinct math) |
+----------------------+
| 5 |
+----------------------+
1 row in set (0.00 sec)
-- SUM(math) 统计的是成绩总和
mysql> select sum(math) from score;
+-----------+
| sum(math) |
+-----------+
| 589 |
+-----------+
1 row in set (0.00 sec)
-- 不及格 < 60 的总分,没有结果,返回 NULL'
mysql> select sum(math) from score where math < 60;
+-----------+
| sum(math) |
+-----------+
| NULL |
+-----------+
1 row in set (0.00 sec)
-- 统计平均总分
mysql> select avg(chinese+math+english) 平均总分 from score;
+-------------------+
| 平均总分 |
+-------------------+
| 298.6666666666667 |
+-------------------+
1 row in set (0.00 sec)
-- 返回英语最高分
SELECT MAX(english) F
mysql> select max(english) 英语最高分 from score;
+-----------------+
| 英语最高分 |
+-----------------+
| 90 |
+-----------------+
1 row in set (0.00 sec)
-- 返回 > 70 分以上的数学最低分
mysql> select min(math) 数学大于七十的最低分 from score where math > 70;
+-----------------------------------+
| 数学大于七十的最低分 |
+-----------------------------------+
| 78 |
+-----------------------------------+
1 row in set (0.00 sec)
七,group by子句的使用
在select中使用group by 子句可以对指定列进行分组查询
语法:select column1, column2, .. from table group by column;
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.01 sec)
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)
mysql> select deptno, job, avg(sal), max(sal) from emp group by deptno ,job;
+--------+-----------+-------------+----------+
| deptno | job | avg(sal) | max(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 | 1100.00 |
| 20 | MANAGER | 2975.000000 | 2975.00 |
| 30 | CLERK | 950.000000 | 950.00 |
| 30 | MANAGER | 2850.000000 | 2850.00 |
| 30 | SALESMAN | 1400.000000 | 1600.00 |
+--------+-----------+-------------+----------+
9 rows in set (0.00 sec)
mysql> select deptno, job, avg(sal), max(sal) from emp group by deptno ,job;
+--------+-----------+-------------+----------+
| deptno | job | avg(sal) | max(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 | 1100.00 |
| 20 | MANAGER | 2975.000000 | 2975.00 |
| 30 | CLERK | 950.000000 | 950.00 |
| 30 | MANAGER | 2850.000000 | 2850.00 |
| 30 | SALESMAN | 1400.000000 | 1600.00 |
+--------+-----------+-------------+----------+
9 rows in set (0.00 sec)
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)
mysql> select deptno,avg(sal) as myavg from emp group by deptno having myavg < 2000;
+--------+-------------+
| deptno | myavg |
+--------+-------------+
| 30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)
mysql> select * from emp where sal > 2000;
+--------+-------+-----------+------+---------------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+-------+-----------+------+---------------------+---------+------+--------+
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 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 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
+--------+-------+-----------+------+---------------------+---------+------+--------+
6 rows in set (0.00 sec)
mysql> select deptno, avg(sal) from emp where sal > 1000 group by deptno;
+--------+-------------+
| deptno | avg(sal) |
+--------+-------------+
| 10 | 2916.666667 |
| 20 | 2518.750000 |
| 30 | 1690.000000 |
+--------+-------------+
3 rows in set (0.00 sec)
mysql> select deptno, avg(sal) as myavg from emp where sal > 1000 group by deptno having myavg < 2000;
+--------+-------------+
| deptno | myavg |
+--------+-------------+
| 30 | 1690.000000 |
+--------+-------------+
1 row in set (0.00 sec)
总结:
1. groupby是通过分组这样的手段,为未来进行聚合统计提供基本的功能支持,(groupby一定是配合聚合统计使用的)。
2. groupby后面跟的都是分组的字段依据,只有在groupby后面出现的字段,未来在聚合统计的时候,在select中才能出现。
3. whereVShaving:他们两个不是冲突的,是互相补充的
having通常:是在完成整个分组聚合统计,然后再进行筛选。
where通常:是在表中数据初步被筛选的时候,要起效果的。
点个赞吧!!!