【MySQL】第九弹---掌握SQL关键操作:更新、删除、插入与聚合分析的秘诀

news/2025/2/25 6:57:35

个人主页: 熬夜学编程的小林

💗系列专栏: 【C语言详解】 【数据结构详解】【C++详解】【Linux系统编程】【MySQL】

目录

1 Update

2 Delete

2.1 删除数据

2.2 截断表

3 插入查询结果

4 聚合函数

5 group by子句的使用


1 Update

语法:

UPDATE table_name SET column = expr [, column = expr ...]
    [WHERE ...] [ORDER BY ...] [LIMIT ...]

对查询到的结果进行列值更新

案例:

  • 将孙悟空同学的数学成绩变更为 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, 1 row 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.00 sec)
  • 将曹孟德同学的数学成绩变更为 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.01 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 位同学的数学成绩加上 30 分
# 查看倒数三名信息
mysql> select name,chinese+math+english total from exam_result order by total limit 3;
+-----------+-------+
| name      | total |
+-----------+-------+
| 宋公明    |   170 |
| 刘玄德    |   185 |
| 曹孟德    |   197 |
+-----------+-------+
3 rows in set (0.00 sec)

# 修改数学成绩
mysql> update exam_result set math=math+30 order by chinese+math+english limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

# 修改成绩厚的后三名成绩发生了变化
mysql> select name,chinese+math+english total from exam_result ordaer by total limit 3;
+-----------+-------+
| name      | total |
+-----------+-------+
| 宋公明    |   200 |
| 刘玄德    |   215 |
| 唐三藏    |   221 |
+-----------+-------+
3 rows in set (0.00 sec)

mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   80 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      70 |   90 |      67 |
|  5 | 刘玄德    |      55 |  115 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   95 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)

注意:mysql不支持+=的语法。

  • 将所有同学的语文成绩更新为原来的 2 倍

注意:更新全表的语句慎用!

# 查看原始信息
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   80 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      70 |   90 |      67 |
|  5 | 刘玄德    |      55 |  115 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   95 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)

# 更新语文成绩
mysql> update exam_result set chinese = chinese * 2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7  Changed: 7  Warnings: 0

# 查看修改后的成绩
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |     134 |   98 |      56 |
|  2 | 孙悟空    |     174 |   80 |      77 |
|  3 | 猪悟能    |     176 |   98 |      90 |
|  4 | 曹孟德    |     140 |   90 |      67 |
|  5 | 刘玄德    |     110 |  115 |      45 |
|  6 | 孙权      |     140 |   73 |      78 |
|  7 | 宋公明    |     150 |   95 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)

2 Delete

2.1 删除数据

语法:

DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]

案例:

  • 删除孙悟空同学的考试成绩
# 查看name = 孙悟空的考试成绩
mysql> select * from exam_result where name = '孙悟空';
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  2 | 孙悟空    |     174 |   80 |      77 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)

# 删除孙悟空的考试成绩
mysql> delete from exam_result where name = '孙悟空';
Query OK, 1 row affected (0.01 sec)

# 查看删除后孙悟空的信息
mysql> select * from exam_result where name = '孙悟空';
Empty set (0.00 sec)
  • 删除整张表数据

注意:删除整表操作要慎用!

创建表

mysql> create table for_delete (
    id int primary key auto_increment,
    name varchar(20)
    );

插入数据

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)

删除表数据

# 删除整个表的数据
mysql> delete from for_delete;
Query OK, 3 rows affected (0.00 sec)

# 查看表的数据
mysql> select * from for_delete;
Empty set (0.01 sec)

再插入一条数据

mysql> insert into for_delete (name) values('D');
Query OK, 1 row affected (0.01 sec)

# 插入的数据,id从4开始
mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  4 | D    |
+----+------+
1 row in set (0.00 sec)

# 查看表创建结构,auto_increment = 5
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)
使用delele删除整个表的数据,不会重置auto_increment的值。

2.2 截断表

语法: 

TRUNCATE [TABLE] 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.03 sec)

mysql> desc for_truncate;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

插入数据

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.04 sec)

mysql> select * from for_truncate;
Empty set (0.00 sec)

再插入一条数据

mysql> insert into for_truncate (name) values('D');
Query OK, 1 row affected (0.01 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)

3 插入查询结果

语法:

INSERT INTO table_name [(column [, column ...])] SELECT ...

案例:

  • 删除表中的的重复记录,重复的数据只能有一份

思路

1、创建一个原表结构相同的表

2、将原表去重的数据插入到新表

3、将原表改为其他名字(备份原表数据)

4、将新表名改为原表名

创建一个重复数据的表

mysql> create table duplicate_table (
    -> id int,
    -> name varchar(20)
    -> );
Query OK, 0 rows affected (0.03 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)

创建一个原表结构相同的表

mysql> create table no_duplicate_table like duplicate_table;
Query OK, 0 rows affected (0.03 sec)

mysql> desc no_duplicate_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc duplicate_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

将原表去重的数据插入到新表

mysql> insert into no_duplicate_table select distinct * from dupliccate_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)

将原表改为其他名字(备份原表数据)

mysql> rename table duplicate_table to tmp_table;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+--------------------+
| Tables_in_test5_db |
+--------------------+
| exam_result        |
| for_delete         |
| for_truncate       |
| no_duplicate_table |
| student            |
| tmp_table          |
+--------------------+
6 rows in set (0.00 sec)

将新表名改为原表名

mysql> rename table no_duplicate_table to duplicate_table;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+
3 rows in set (0.00 sec)

4 聚合函数

函数说明
COUNT([DISTINCT] expr)返回查询到的数据的 数量
SUM([DISTINCT] expr)返回查询到的数据的 总和,不是数字没有意义
AVG([DISTINCT] expr)返回查询到的数据的 平均值,不是数字没有意义
MAX([DISTINCT] expr)返回查询到的数据的 最大值,不是数字没有意义
MIN([DISTINCT] expr)返回查询到的数据的 最小值,不是数字没有意义

案例:

  • 统计班级共有多少同学
# 使用 * 统计,不受NULL限制
mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

# 查看原表数据
mysql> select * from student;
+----+-----+--------+--------+
| id | sn  | name   | qq     |
+----+-----+--------+--------+
|  1 | 123 | 张三   | 123456 |
|  2 | 124 | 李四   | 156954 |
|  3 | 125 | 王五   | 523124 |
|  4 | 126 | 孙权   | NULL   |
|  5 | 127 | 妲己   | NULL   |
+----+-----+--------+--------+
5 rows in set (0.00 sec)

# 使用表达式统计
mysql> select count(1) from student;
+----------+
| count(1) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)
  • 统计班级收集的 qq 号有多少
# NULL 不会计入结果
mysql> select count(qq) from student;
+-----------+
| count(qq) |
+-----------+
|         3 |
+-----------+
1 row in set (0.00 sec)

mysql> select * from student;
+----+-----+--------+--------+
| id | sn  | name   | qq     |
+----+-----+--------+--------+
|  1 | 123 | 张三   | 123456 |
|  2 | 124 | 李四   | 156954 |
|  3 | 125 | 王五   | 523124 |
|  4 | 126 | 孙权   | NULL   |
|  5 | 127 | 妲己   | NULL   |
+----+-----+--------+--------+
5 rows in set (0.00 sec)
  • 统计本次考试的数学成绩分数个数
mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
|           6 |
+-------------+
1 row in set (0.00 sec)

mysql> select math from exam_result; # 直接查询会有重复的成绩,需要去重
+------+
| math |
+------+
|   98 |
|   98 |
|   90 |
|  115 |
|   73 |
|   95 |
+------+
6 rows in set (0.00 sec)
# count(distinct math)统计的是去重成绩数量
mysql> select count(distinct math) from exam_result;
+----------------------+
| count(distinct math) |
+----------------------+
|                    5 |
+----------------------+
1 row in set (0.00 sec)
  • 统计数学成绩总分
mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
|       569 |
+-----------+
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)
  •  统计平均总分
mysql> select avg(chinese+math+english) 平均总分 from exam_result; 
+--------------+
| 平均总分     |
+--------------+
|        297.5 |
+--------------+
1 row in set (0.00 sec)
  • 返回英语最高分
mysql> select max(english) 英语最高分 from exam_result;
+-----------------+
| 英语最高分      |
+-----------------+
|              90 |
+-----------------+
1 row in set (0.00 sec)
  • 返回 > 70 分以上的数学最低分
mysql> select min(math) from exam_result where math > 70;
+-----------+
| min(math) |
+-----------+
|        73 |
+-----------+
1 row in set (0.00 sec)

5 group by子句的使用

在select中使用group by 子句可以对指定列进行分组查询。

select column1, column2, .. from table group by column;

案例:
准备工作,创建一个雇员信息表(来自oracle 9i的经典测试表)

  • EMP员工表
  • DEPT部门表
  • SALGRADE工资等级表
# 将表的数据导入
source /root/mysql/scott_data.sql

# 查看当前数据库
mysql> select database();
+------------+
| database() |
+------------+
| scott      |
+------------+
1 row in set (0.00 sec)

# 查看表
mysql> show tables;
+-----------------+
| Tables_in_scott |
+-----------------+
| dept            |
| emp             |
| salgrade        |
+-----------------+
3 rows in set (0.00 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)
  • 显示平均工资低于2000的部门和它的平均工资

having和group by配合使用,对group by结果进行过滤。

mysql> select deptno,avg(sal) myavg from emp group by deptno having myavg < 2000;
+--------+-------------+
| deptno | myavg       |
+--------+-------------+
|     30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)

执行顺序分析

mysql> select deptno,job,avg(sal) myavg from emp where ename != 'SMITH' group by deptno,job having myavg < 2000;
+--------+----------+-------------+
| deptno | job      | myavg       |
+--------+----------+-------------+
|     10 | CLERK    | 1300.000000 |
|     20 | CLERK    | 1100.000000 |
|     30 | CLERK    |  950.000000 |
|     30 | SALESMAN | 1400.000000 |
+--------+----------+-------------+
4 rows in set (0.00 sec)


http://www.niftyadmin.cn/n/5865124.html

相关文章

【AI+智造】DeepSeek价值重构:当采购与物控遇上数字化转型的化学反应

作者&#xff1a;Odoo技术开发/资深信息化负责人 日期&#xff1a;2025年2月24日 引言&#xff1a;从事企业信息化工作16年&#xff0c;我见证过无数企业从手工台账到ERP系统的跨越。但真正让采购和物控部门脱胎换骨的&#xff0c;是融合了Deepseek AI的Odoo数字化解决方案——…

如何在 CMake 上设置新项目以获得成功

选择正确的构建系统可以决定项目的成功与否。从处理依赖项到确保正确编译和链接代码&#xff0c;正确的构建系统可以节省 你时间并避免潜在的麻烦。 在众多可用选项中&#xff0c;CMake 构建系统脱颖而出&#xff0c;可以处理复杂的跨平台项目。 本教程介绍了 CMake 的受欢迎…

【C】堆的应用1 -- 堆排序

之前学习了堆&#xff0c;堆的一棵以顺序结构存储的完全二叉树&#xff0c;堆本身又氛围大根堆和小根堆&#xff0c;假设以大根堆为例&#xff0c;由于堆顶部元素是一棵二叉树里面最大的元素&#xff0c;所以如果每次都取堆顶的元素&#xff0c;那么取出的元素就是一个降序排列…

量子计算的数学基础:复数、矩阵和线性代数

量子计算是基于量子力学原理的一种新型计算模式,它与经典计算机在信息处理的方式上有着根本性的区别。在量子计算中,信息的最小单位是量子比特(qubit),而不是传统计算中的比特。量子比特的状态是通过量子力学中的数学工具来描述的,因此,理解量子计算的数学基础对于深入学…

【Java项目】基于Spring Boot的家具销售电商系统

【Java项目】基于Spring Boot的家具销售电商系统 技术简介&#xff1a;采用Spring Boot框架、Java技术、MySQL数据库等实现。 系统简介&#xff1a;家具销售电商系统主要实现了管理员模块、用户模块二大部分。1、管理员&#xff1a;首页、个人中心、家具分类管理、热销家具管理…

DeepSeek技术全景解析:架构创新与行业差异化竞争力

一、DeepSeek技术体系的核心突破 架构设计&#xff1a;效率与性能的双重革新 Multi-head Latent Attention (MLA)&#xff1a;通过将注意力头维度与隐藏层解耦&#xff0c;实现显存占用降低30%的同时支持4096超长上下文窗口。深度优化的MoE架构&#xff1a;结合256个路由专家…

ONNX转RKNN的环境搭建和部署流程

将ONNX模型转换为RKNN模型的过程记录 工具准备 rknn-toolkit:https://github.com/rockchip-linux/rknn-toolkit rknn-toolkit2:https://github.com/airockchip/rknn-toolkit2 rknn_model_zoo:https://github.com/airockchip/rknn_model_zoo ultralytics_yolov8:https://github…

解锁CSnakes:.NET与Python的融合魔法

一、引言 在软件开发的广袤领域中&#xff0c;我们常常面临各种复杂的业务需求和技术挑战。不同的编程语言犹如各具特色的工具&#xff0c;它们在不同的场景下展现出独特的优势。例如&#xff0c;C# 以其强大的类型系统和丰富的类库&#xff0c;在企业级应用开发中占据重要地位…