<6>-MySQL表的增删查改
目录
一,create(创建表)
二,retrieve(查询表)
1,select列
2,where条件
三,update(更新表)
四,delete(删除表)
五,插入查询结果
六,聚合统计函数
七,group by子句的使用
一,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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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: 0mysql> 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通常:是在表中数据初步被筛选的时候,要起效果的。
点个赞吧!!!
相关文章:
<6>-MySQL表的增删查改
目录 一,create(创建表) 二,retrieve(查询表) 1,select列 2,where条件 三,update(更新表) 四,delete(删除表…...

大话软工笔记—需求分析概述
需求分析,就是要对需求调研收集到的资料信息逐个地进行拆分、研究,从大量的不确定“需求”中确定出哪些需求最终要转换为确定的“功能需求”。 需求分析的作用非常重要,后续设计的依据主要来自于需求分析的成果,包括: 项目的目的…...
云计算——弹性云计算器(ECS)
弹性云服务器:ECS 概述 云计算重构了ICT系统,云计算平台厂商推出使得厂家能够主要关注应用管理而非平台管理的云平台,包含如下主要概念。 ECS(Elastic Cloud Server):即弹性云服务器,是云计算…...

51c自动驾驶~合集58
我自己的原文哦~ https://blog.51cto.com/whaosoft/13967107 #CCA-Attention 全局池化局部保留,CCA-Attention为LLM长文本建模带来突破性进展 琶洲实验室、华南理工大学联合推出关键上下文感知注意力机制(CCA-Attention),…...

Prompt Tuning、P-Tuning、Prefix Tuning的区别
一、Prompt Tuning、P-Tuning、Prefix Tuning的区别 1. Prompt Tuning(提示调优) 核心思想:固定预训练模型参数,仅学习额外的连续提示向量(通常是嵌入层的一部分)。实现方式:在输入文本前添加可训练的连续向量(软提示),模型只更新这些提示参数。优势:参数量少(仅提…...

树莓派超全系列教程文档--(61)树莓派摄像头高级使用方法
树莓派摄像头高级使用方法 配置通过调谐文件来调整相机行为 使用多个摄像头安装 libcam 和 rpicam-apps依赖关系开发包 文章来源: http://raspberry.dns8844.cn/documentation 原文网址 配置 大多数用例自动工作,无需更改相机配置。但是,一…...

Spark 之 入门讲解详细版(1)
1、简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室(Algorithms, Machines, and People Lab)开发通用内存并行计算框架。Spark在2013年6月进入Apache成为孵化项目,8个月后成为Apache顶级项目,速度之快足见过人之处&…...
Java 语言特性(面试系列1)
一、面向对象编程 1. 封装(Encapsulation) 定义:将数据(属性)和操作数据的方法绑定在一起,通过访问控制符(private、protected、public)隐藏内部实现细节。示例: public …...
进程地址空间(比特课总结)
一、进程地址空间 1. 环境变量 1 )⽤户级环境变量与系统级环境变量 全局属性:环境变量具有全局属性,会被⼦进程继承。例如当bash启动⼦进程时,环 境变量会⾃动传递给⼦进程。 本地变量限制:本地变量只在当前进程(ba…...

css实现圆环展示百分比,根据值动态展示所占比例
代码如下 <view class""><view class"circle-chart"><view v-if"!!num" class"pie-item" :style"{background: conic-gradient(var(--one-color) 0%,#E9E6F1 ${num}%),}"></view><view v-else …...
脑机新手指南(八):OpenBCI_GUI:从环境搭建到数据可视化(下)
一、数据处理与分析实战 (一)实时滤波与参数调整 基础滤波操作 60Hz 工频滤波:勾选界面右侧 “60Hz” 复选框,可有效抑制电网干扰(适用于北美地区,欧洲用户可调整为 50Hz)。 平滑处理&…...
Leetcode 3576. Transform Array to All Equal Elements
Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接:3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到…...
ubuntu搭建nfs服务centos挂载访问
在Ubuntu上设置NFS服务器 在Ubuntu上,你可以使用apt包管理器来安装NFS服务器。打开终端并运行: sudo apt update sudo apt install nfs-kernel-server创建共享目录 创建一个目录用于共享,例如/shared: sudo mkdir /shared sud…...
椭圆曲线密码学(ECC)
一、ECC算法概述 椭圆曲线密码学(Elliptic Curve Cryptography)是基于椭圆曲线数学理论的公钥密码系统,由Neal Koblitz和Victor Miller在1985年独立提出。相比RSA,ECC在相同安全强度下密钥更短(256位ECC ≈ 3072位RSA…...

(十)学生端搭建
本次旨在将之前的已完成的部分功能进行拼装到学生端,同时完善学生端的构建。本次工作主要包括: 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

【OSG学习笔记】Day 18: 碰撞检测与物理交互
物理引擎(Physics Engine) 物理引擎 是一种通过计算机模拟物理规律(如力学、碰撞、重力、流体动力学等)的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互,广泛应用于 游戏开发、动画制作、虚…...

突破不可导策略的训练难题:零阶优化与强化学习的深度嵌合
强化学习(Reinforcement Learning, RL)是工业领域智能控制的重要方法。它的基本原理是将最优控制问题建模为马尔可夫决策过程,然后使用强化学习的Actor-Critic机制(中文译作“知行互动”机制),逐步迭代求解…...
golang循环变量捕获问题
在 Go 语言中,当在循环中启动协程(goroutine)时,如果在协程闭包中直接引用循环变量,可能会遇到一个常见的陷阱 - 循环变量捕获问题。让我详细解释一下: 问题背景 看这个代码片段: fo…...

Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动
一、前言说明 在2011版本的gb28181协议中,拉取视频流只要求udp方式,从2016开始要求新增支持tcp被动和tcp主动两种方式,udp理论上会丢包的,所以实际使用过程可能会出现画面花屏的情况,而tcp肯定不丢包,起码…...
树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频
使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源: http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...
DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径
目录 一、智慧能源微电网优化调度概述1.1 智慧能源微电网概念1.2 优化调度的重要性1.3 目前面临的挑战 二、DeepSeek 技术探秘2.1 DeepSeek 技术原理2.2 DeepSeek 独特优势2.3 DeepSeek 在 AI 领域地位 三、DeepSeek 在微电网优化调度中的应用剖析3.1 数据处理与分析3.2 预测与…...

基于距离变化能量开销动态调整的WSN低功耗拓扑控制开销算法matlab仿真
目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.算法仿真参数 5.算法理论概述 6.参考文献 7.完整程序 1.程序功能描述 通过动态调整节点通信的能量开销,平衡网络负载,延长WSN生命周期。具体通过建立基于距离的能量消耗模型&am…...

简易版抽奖活动的设计技术方案
1.前言 本技术方案旨在设计一套完整且可靠的抽奖活动逻辑,确保抽奖活动能够公平、公正、公开地进行,同时满足高并发访问、数据安全存储与高效处理等需求,为用户提供流畅的抽奖体验,助力业务顺利开展。本方案将涵盖抽奖活动的整体架构设计、核心流程逻辑、关键功能实现以及…...
rknn优化教程(二)
文章目录 1. 前述2. 三方库的封装2.1 xrepo中的库2.2 xrepo之外的库2.2.1 opencv2.2.2 rknnrt2.2.3 spdlog 3. rknn_engine库 1. 前述 OK,开始写第二篇的内容了。这篇博客主要能写一下: 如何给一些三方库按照xmake方式进行封装,供调用如何按…...

K8S认证|CKS题库+答案| 11. AppArmor
目录 11. AppArmor 免费获取并激活 CKA_v1.31_模拟系统 题目 开始操作: 1)、切换集群 2)、切换节点 3)、切换到 apparmor 的目录 4)、执行 apparmor 策略模块 5)、修改 pod 文件 6)、…...
C++:std::is_convertible
C++标志库中提供is_convertible,可以测试一种类型是否可以转换为另一只类型: template <class From, class To> struct is_convertible; 使用举例: #include <iostream> #include <string>using namespace std;struct A { }; struct B : A { };int main…...
React hook之useRef
React useRef 详解 useRef 是 React 提供的一个 Hook,用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途,下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…...
反向工程与模型迁移:打造未来商品详情API的可持续创新体系
在电商行业蓬勃发展的当下,商品详情API作为连接电商平台与开发者、商家及用户的关键纽带,其重要性日益凸显。传统商品详情API主要聚焦于商品基本信息(如名称、价格、库存等)的获取与展示,已难以满足市场对个性化、智能…...
Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以?
Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以? 在 Golang 的面试中,map 类型的使用是一个常见的考点,其中对 key 类型的合法性 是一道常被提及的基础却很容易被忽视的问题。本文将带你深入理解 Golang 中…...

【Oracle APEX开发小技巧12】
有如下需求: 有一个问题反馈页面,要实现在apex页面展示能直观看到反馈时间超过7天未处理的数据,方便管理员及时处理反馈。 我的方法:直接将逻辑写在SQL中,这样可以直接在页面展示 完整代码: SELECTSF.FE…...