【MySQL】 基本查询(下)

打印 上一主题 下一主题

主题 905|帖子 905|积分 2715

接待拜访:雾里看山-CSDN博客
本篇主题:【MySQL】 基本查询(下)
发布时间:2025.2.18
隶属专栏:MySQL
  


  
Update

语法

  1. UPDATE table_name SET column = expr [, column = expr ...]
  2.         [WHERE ...] [ORDER BY ...] [LIMIT ...]
复制代码
对查询到的结果进行列值更新
案例


  • 将孙悟空同学的数学成绩变动为 80 分
  1. mysql> select name, math from exam_result;
  2. +-----------+------+
  3. | name      | math |
  4. +-----------+------+
  5. | 唐三藏    |   98 |
  6. | 孙悟空    |   78 |
  7. | 猪悟能    |   98 |
  8. | 曹孟德    |   84 |
  9. | 刘玄德    |   85 |
  10. | 孙权      |   73 |
  11. | 宋公明    |   65 |
  12. +-----------+------+
  13. 7 rows in set (0.00 sec)
  14. mysql> update exam_result set math=80 where name='孙悟空';
  15. Query OK, 1 row affected (0.00 sec)
  16. Rows matched: 1  Changed: 1  Warnings: 0
  17. mysql> select name, math from exam_result;
  18. +-----------+------+
  19. | name      | math |
  20. +-----------+------+
  21. | 唐三藏    |   98 |
  22. | 孙悟空    |   80 |
  23. | 猪悟能    |   98 |
  24. | 曹孟德    |   84 |
  25. | 刘玄德    |   85 |
  26. | 孙权      |   73 |
  27. | 宋公明    |   65 |
  28. +-----------+------+
  29. 7 rows in set (0.00 sec)
复制代码

  • 将曹孟德同学的数学成绩变动为 60 分,语文成绩变动为 70 分
  1. mysql> select * from exam_result;
  2. +----+-----------+---------+------+---------+
  3. | id | name      | chinese | math | english |
  4. +----+-----------+---------+------+---------+
  5. |  1 | 唐三藏    |      67 |   98 |      56 |
  6. |  2 | 孙悟空    |      87 |   80 |      77 |
  7. |  3 | 猪悟能    |      88 |   98 |      90 |
  8. |  4 | 曹孟德    |      82 |   84 |      67 |
  9. |  5 | 刘玄德    |      55 |   85 |      45 |
  10. |  6 | 孙权      |      70 |   73 |      78 |
  11. |  7 | 宋公明    |      75 |   65 |      30 |
  12. +----+-----------+---------+------+---------+
  13. 7 rows in set (0.00 sec)
  14. mysql> update exam_result set math=60, chinese=70  where name='曹孟德';
  15. Query OK, 1 row affected (0.00 sec)
  16. Rows matched: 1  Changed: 1  Warnings: 0
  17. mysql> select * from exam_result;
  18. +----+-----------+---------+------+---------+
  19. | id | name      | chinese | math | english |
  20. +----+-----------+---------+------+---------+
  21. |  1 | 唐三藏    |      67 |   98 |      56 |
  22. |  2 | 孙悟空    |      87 |   80 |      77 |
  23. |  3 | 猪悟能    |      88 |   98 |      90 |
  24. |  4 | 曹孟德    |      70 |   60 |      67 |
  25. |  5 | 刘玄德    |      55 |   85 |      45 |
  26. |  6 | 孙权      |      70 |   73 |      78 |
  27. |  7 | 宋公明    |      75 |   65 |      30 |
  28. +----+-----------+---------+------+---------+
  29. 7 rows in set (0.00 sec)
复制代码

  • 将总成绩倒数前三的 3 位同学的数学成绩加上 30 分
  1. mysql> select name, math+chinese+english total from exam_result order by total asc limit 3;
  2. +-----------+-------+
  3. | name      | total |
  4. +-----------+-------+
  5. | 宋公明    |   170 |
  6. | 刘玄德    |   185 |
  7. | 曹孟德    |   197 |
  8. +-----------+-------+
  9. 3 rows in set (0.00 sec)
  10. mysql> update exam_result set math=math+30 order by chinese+math+english limit 3;
  11. Query OK, 3 rows affected (0.02 sec)
  12. Rows matched: 3  Changed: 3  Warnings: 0
  13. mysql> select name, math+chinese+english total from exam_result;
  14. +-----------+-------+
  15. | name      | total |
  16. +-----------+-------+
  17. | 唐三藏    |   221 |
  18. | 孙悟空    |   244 |
  19. | 猪悟能    |   276 |
  20. | 曹孟德    |   227 |
  21. | 刘玄德    |   215 |
  22. | 孙权      |   221 |
  23. | 宋公明    |   200 |
  24. +-----------+-------+
  25. 7 rows in set (0.00 sec)
复制代码

  • 将所有同学的语文成绩更新为原来的 2 倍
注意:更新全表的语句慎用!
  1. mysql> select * from exam_result;
  2. +----+-----------+---------+------+---------+
  3. | id | name      | chinese | math | english |
  4. +----+-----------+---------+------+---------+
  5. |  1 | 唐三藏    |      67 |   98 |      56 |
  6. |  2 | 孙悟空    |      87 |   80 |      77 |
  7. |  3 | 猪悟能    |      88 |   98 |      90 |
  8. |  4 | 曹孟德    |      70 |   90 |      67 |
  9. |  5 | 刘玄德    |      55 |  115 |      45 |
  10. |  6 | 孙权      |      70 |   73 |      78 |
  11. |  7 | 宋公明    |      75 |   95 |      30 |
  12. +----+-----------+---------+------+---------+
  13. 7 rows in set (0.00 sec)
  14. mysql> update exam_result set chinese=chinese*2;
  15. Query OK, 7 rows affected (0.00 sec)
  16. Rows matched: 7  Changed: 7  Warnings: 0
  17. mysql> select * from exam_result;
  18. +----+-----------+---------+------+---------+
  19. | id | name      | chinese | math | english |
  20. +----+-----------+---------+------+---------+
  21. |  1 | 唐三藏    |     134 |   98 |      56 |
  22. |  2 | 孙悟空    |     174 |   80 |      77 |
  23. |  3 | 猪悟能    |     176 |   98 |      90 |
  24. |  4 | 曹孟德    |     140 |   90 |      67 |
  25. |  5 | 刘玄德    |     110 |  115 |      45 |
  26. |  6 | 孙权      |     140 |   73 |      78 |
  27. |  7 | 宋公明    |     150 |   95 |      30 |
  28. +----+-----------+---------+------+---------+
  29. 7 rows in set (0.00 sec)
复制代码
Delete

删除数据

语法

  1. DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
复制代码
案例


  • 删除孙悟空同学的测验成绩
  1. mysql> select * from exam_result where name='孙悟空';
  2. +----+-----------+---------+------+---------+
  3. | id | name      | chinese | math | english |
  4. +----+-----------+---------+------+---------+
  5. |  2 | 孙悟空    |     174 |   80 |      77 |
  6. +----+-----------+---------+------+---------+
  7. 1 row in set (0.00 sec)
  8. mysql> delete from exam_result where name='孙悟空';
  9. Query OK, 1 row affected (0.00 sec)
  10. mysql> select * from exam_result;
  11. +----+-----------+---------+------+---------+
  12. | id | name      | chinese | math | english |
  13. +----+-----------+---------+------+---------+
  14. |  1 | 唐三藏    |     134 |   98 |      56 |
  15. |  3 | 猪悟能    |     176 |   98 |      90 |
  16. |  4 | 曹孟德    |     140 |   90 |      67 |
  17. |  5 | 刘玄德    |     110 |  115 |      45 |
  18. |  6 | 孙权      |     140 |   73 |      78 |
  19. |  7 | 宋公明    |     150 |   95 |      30 |
  20. +----+-----------+---------+------+---------+
  21. 6 rows in set (0.00 sec)
复制代码

  • 删除倒数第一名
  1. mysql> select name, chinese+math+english total from exam_result order by total asc limit 1;
  2. +-----------+-------+
  3. | name      | total |
  4. +-----------+-------+
  5. | 刘玄德    |   270 |
  6. +-----------+-------+
  7. 1 row in set (0.00 sec)
  8. mysql> delete from exam_result order by chinese+math+english asc limit 1;
  9. Query OK, 1 row affected (0.01 sec)
  10. mysql> select * from exam_result;
  11. +----+-----------+---------+------+---------+
  12. | id | name      | chinese | math | english |
  13. +----+-----------+---------+------+---------+
  14. |  1 | 唐三藏    |     134 |   98 |      56 |
  15. |  3 | 猪悟能    |     176 |   98 |      90 |
  16. |  4 | 曹孟德    |     140 |   90 |      67 |
  17. |  6 | 孙权      |     140 |   73 |      78 |
  18. |  7 | 宋公明    |     150 |   95 |      30 |
  19. +----+-----------+---------+------+---------+
  20. 5 rows in set (0.00 sec)
复制代码

  • 删除整张表数据
注意:删除整表操纵要慎用!
创建表结构插入数据并查看。
  1. mysql> CREATE TABLE for_delete (
  2.     -> id INT PRIMARY KEY AUTO_INCREMENT,
  3.     -> name VARCHAR(20)
  4.     -> );
  5. Query OK, 0 rows affected (0.02 sec)
  6. mysql> INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
  7. Query OK, 3 rows affected (0.01 sec)
  8. Records: 3  Duplicates: 0  Warnings: 0
  9. mysql> select * from for_delete;
  10. +----+------+
  11. | id | name |
  12. +----+------+
  13. |  1 | A    |
  14. |  2 | B    |
  15. |  3 | C    |
  16. +----+------+
  17. 3 rows in set (0.00 sec)
  18. mysql> show create table for_delete\G
  19. *************************** 1. row ***************************
  20.        Table: for_delete
  21. Create Table: CREATE TABLE `for_delete` (
  22.   `id` int(11) NOT NULL AUTO_INCREMENT,
  23.   `name` varchar(20) DEFAULT NULL,
  24.   PRIMARY KEY (`id`)
  25. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
  26. 1 row in set (0.00 sec)
复制代码
删除数据
  1. mysql> delete from for_delete;
  2. Query OK, 3 rows affected (0.01 sec)
  3. mysql> select * from for_delete;
  4. Empty set (0.00 sec)
复制代码
插入数据并查看表结构
  1. mysql> insert into for_delete (name) values ('D');
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> show create table for_delete\G
  4. *************************** 1. row ***************************
  5.        Table: for_delete
  6. Create Table: CREATE TABLE `for_delete` (
  7.   `id` int(11) NOT NULL AUTO_INCREMENT,
  8.   `name` varchar(20) DEFAULT NULL,
  9.   PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
  11. 1 row in set (0.00 sec)
  12. mysql> select * from for_delete;
  13. +----+------+
  14. | id | name |
  15. +----+------+
  16. |  4 | D    |
  17. +----+------+
  18. 1 row in set (0.00 sec)
复制代码
截断表

语法

  1. TRUNCATE [TABLE] table_name
复制代码
注意:这个操纵慎用

  • 只能对整表操纵,不能像 DELETE 一样针对部分数据操纵;
  • 实际上 MySQL 不对数据操纵,所以比 DELETE 更快,但是TRUNCATE在删除数据的时候,并不经过真正的事物,所以无法回滚
  • 会重置 AUTO_INCREMENT 项
案例

创建表结构并插入数据
  1. mysql> CREATE TABLE for_truncate (
  2.     -> id INT PRIMARY KEY AUTO_INCREMENT,
  3.     -> name VARCHAR(20)
  4.     -> );
  5. Query OK, 0 rows affected (0.01 sec)
  6. mysql> INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C');
  7. Query OK, 3 rows affected (0.00 sec)
  8. Records: 3  Duplicates: 0  Warnings: 0
  9. mysql> select * from for_truncate;
  10. +----+------+
  11. | id | name |
  12. +----+------+
  13. |  1 | A    |
  14. |  2 | B    |
  15. |  3 | C    |
  16. +----+------+
  17. 3 rows in set (0.00 sec)
复制代码
截断整表数据,注意影响行数是 0,所以实际上没有对数据真正操纵
  1. mysql> truncate for_truncate;
  2. Query OK, 0 rows affected (0.03 sec)
  3. mysql> select * from for_truncate;
  4. Empty set (0.00 sec)
复制代码
再插入一条数据,自增 id 在重新增长
  1. mysql> insert into for_truncate (name) values ('D');
  2. Query OK, 1 row affected (0.01 sec)
  3. mysql> show create table for_truncate\G
  4. *************************** 1. row ***************************
  5.        Table: for_truncate
  6. Create Table: CREATE TABLE `for_truncate` (
  7.   `id` int(11) NOT NULL AUTO_INCREMENT,
  8.   `name` varchar(20) DEFAULT NULL,
  9.   PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
  11. 1 row in set (0.00 sec)
  12. mysql> select * from for_truncate;
  13. +----+------+
  14. | id | name |
  15. +----+------+
  16. |  1 | D    |
  17. +----+------+
  18. 1 row in set (0.00 sec)
复制代码
插入查询结果

语法

  1. INSERT INTO table_name [(column [, column ...])] SELECT ...
复制代码
案例

删除表中的的重复复记录,重复的数据只能有一份
创建原数据表并插入数据
  1. mysql> CREATE TABLE duplicate_table (id int, name varchar(20));
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> INSERT INTO duplicate_table VALUES
  4.     -> (100, 'aaa'),
  5.     -> (100, 'aaa'),
  6.     -> (200, 'bbb'),
  7.     -> (200, 'bbb'),
  8.     -> (200, 'bbb'),
  9.     -> (300, 'ccc');
  10. Query OK, 6 rows affected (0.01 sec)
  11. Records: 6  Duplicates: 0  Warnings: 0
  12. mysql> desc duplicate_table;
  13. +-------+-------------+------+-----+---------+-------+
  14. | Field | Type        | Null | Key | Default | Extra |
  15. +-------+-------------+------+-----+---------+-------+
  16. | id    | int(11)     | YES  |     | NULL    |       |
  17. | name  | varchar(20) | YES  |     | NULL    |       |
  18. +-------+-------------+------+-----+---------+-------+
  19. 2 rows in set (0.00 sec)
复制代码
创建一张空表 no_duplicate_table,结构和 duplicate_table 一样
  1. mysql> create table no_duplicate_table like duplicate_table;
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> desc no_duplicate_table;
  4. +-------+-------------+------+-----+---------+-------+
  5. | Field | Type        | Null | Key | Default | Extra |
  6. +-------+-------------+------+-----+---------+-------+
  7. | id    | int(11)     | YES  |     | NULL    |       |
  8. | name  | varchar(20) | YES  |     | NULL    |       |
  9. +-------+-------------+------+-----+---------+-------+
  10. 2 rows in set (0.00 sec)
复制代码
将 duplicate_table 的去重数据插入到 no_duplicate_table
  1. mysql> insert into no_duplicate_table select distinct * from duplicate_table;
  2. Query OK, 3 rows affected (0.03 sec)
  3. Records: 3  Duplicates: 0  Warnings: 0
  4. mysql> select * from no_duplicate_table;
  5. +------+------+
  6. | id   | name |
  7. +------+------+
  8. |  100 | aaa  |
  9. |  200 | bbb  |
  10. |  300 | ccc  |
  11. +------+------+
  12. 3 rows in set (0.00 sec)
复制代码
通过重命名表,实现原子的去重操纵
  1. mysql> rename table duplicate_table to old_duplicate_table;
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> rename table no_duplicate_table to duplicate_table;
  4. Query OK, 0 rows affected (0.01 sec)
复制代码
查看最闭幕果
  1. mysql> select * from duplicate_table;
  2. +------+------+
  3. | id   | name |
  4. +------+------+
  5. |  100 | aaa  |
  6. |  200 | bbb  |
  7. |  300 | ccc  |
  8. +------+------+
  9. 3 rows in set (0.01 sec)
复制代码
为什么末了是通过rename的方式进行的?
就是单纯的等统统都就绪了,然后统一放入、更新、见效等。确保操纵是原子的。
聚合函数

函数介绍

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


  • 统计班级共有多少同学
使用 * 做统计,不受 NULL 影响
  1. mysql> select count(*) from exam_result;
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. |        5 |
  6. +----------+
  7. 1 row in set (0.03 sec)
复制代码
使用表达式做统计
  1. mysql> select count(1) from exam_result;
  2. +----------+
  3. | count(1) |
  4. +----------+
  5. |        5 |
  6. +----------+
  7. 1 row in set (0.00 sec)
复制代码

  • 统计班级收集的 qq 号有多少
NULL 不管帐入结果
  1. mysql> select count(qq) from students;
  2. +-----------+
  3. | count(qq) |
  4. +-----------+
  5. |         9 |
  6. +-----------+
  7. 1 row in set (0.00 sec)
复制代码

  • 统计本次测验的数学成绩分数个数
COUNT(math) 统计的是全部成绩
  1. mysql> select count(math) from exam_result;
  2. +-------------+
  3. | count(math) |
  4. +-------------+
  5. |           5 |
  6. +-------------+
  7. 1 row in set (0.00 sec)
复制代码
COUNT(DISTINCT math) 统计的是去重成绩数量
  1. mysql> select count(distinct math) from exam_result;
  2. +----------------------+
  3. | count(distinct math) |
  4. +----------------------+
  5. |                    4 |
  6. +----------------------+
  7. 1 row in set (0.02 sec)
复制代码

  • 统计数学成绩总分
如果没有结果,则返回 NULL
  1. mysql> select sum(math) from exam_result;
  2. +-----------+
  3. | sum(math) |
  4. +-----------+
  5. |       454 |
  6. +-----------+
  7. 1 row in set (0.00 sec)
  8. mysql> select sum(math) from exam_result where math<60;
  9. +-----------+
  10. | sum(math) |
  11. +-----------+
  12. |      NULL |
  13. +-----------+
  14. 1 row in set (0.00 sec)
复制代码

  • 统计均匀总分
  1. mysql> select avg(chinese+math+english) 平均总分 from exam_result;
  2. +--------------+
  3. | 平均总分     |
  4. +--------------+
  5. |          303 |
  6. +--------------+
  7. 1 row in set (0.00 sec)
复制代码

  • 返回英语最高分
  1. mysql> select max(english) from exam_result;
  2. +--------------+
  3. | max(english) |
  4. +--------------+
  5. |           90 |
  6. +--------------+
  7. 1 row in set (0.01 sec)
复制代码

  • 返回 > 70 分以上的英语最低分
  1. mysql> select min(english) from exam_result where english>70;
  2. +--------------+
  3. | min(english) |
  4. +--------------+
  5. |           78 |
  6. +--------------+
  7. 1 row in set (0.00 sec)
复制代码
group by子句的使用

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

  1. select column1, column2, .. from table group by column;
复制代码
分组的目标是为了进行分组以后,方便进行聚合统计
having和where

where:对具体的任意列进行条件筛选
having:对分组聚合之后的结果进行条件筛选
案例


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


  • EMP员工表
  • DEPT部分表
  • SALGRADE工资等级表
具体代码可私我

  • 怎样体现每个部分的均匀工资和最高工资
  1. mysql> select deptno,avg(sal),max(sal) from emp group by deptno;
  2. +--------+-------------+----------+
  3. | deptno | avg(sal)    | max(sal) |
  4. +--------+-------------+----------+
  5. |     10 | 2916.666667 |  5000.00 |
  6. |     20 | 2175.000000 |  3000.00 |
  7. |     30 | 1566.666667 |  2850.00 |
  8. +--------+-------------+----------+
  9. 3 rows in set (0.00 sec)
复制代码

  • 体现每个部分的每种岗位的均匀工资和最低工资
  1. mysql> select avg(sal),min(sal),job, deptno from emp group by deptno, job;
  2. +-------------+----------+-----------+--------+
  3. | avg(sal)    | min(sal) | job       | deptno |
  4. +-------------+----------+-----------+--------+
  5. | 1300.000000 |  1300.00 | CLERK     |     10 |
  6. | 2450.000000 |  2450.00 | MANAGER   |     10 |
  7. | 5000.000000 |  5000.00 | PRESIDENT |     10 |
  8. | 3000.000000 |  3000.00 | ANALYST   |     20 |
  9. |  950.000000 |   800.00 | CLERK     |     20 |
  10. | 2975.000000 |  2975.00 | MANAGER   |     20 |
  11. |  950.000000 |   950.00 | CLERK     |     30 |
  12. | 2850.000000 |  2850.00 | MANAGER   |     30 |
  13. | 1400.000000 |  1250.00 | SALESMAN  |     30 |
  14. +-------------+----------+-----------+--------+
  15. 9 rows in set (0.00 sec)
复制代码

  • 体现均匀工资低于2000的部分和它的均匀工资
统计各个部分的均匀工资
  1. mysql> select avg(sal) from emp group by deptno;
  2. +-------------+
  3. | avg(sal)    |
  4. +-------------+
  5. | 2916.666667 |
  6. | 2175.000000 |
  7. | 1566.666667 |
  8. +-------------+
  9. 3 rows in set (0.00 sec)
复制代码
having和group by共同使用,对group by结果进行过滤
  1. mysql> select avg(sal) as myavg from emp group by deptno having myavg<2000;
  2. +-------------+
  3. | myavg       |
  4. +-------------+
  5. | 1566.666667 |
  6. +-------------+
  7. 1 row in set (0.00 sec)
复制代码
结语

不要单纯的认为,只有磁盘上的表结构导入到MySQL,真实存在的表才叫表。中心筛选出来的,包括最闭幕果,都可以看成逻辑上的表。
MySQL统统皆表
将来我们只要能够处置惩罚好单表的CURD,所有sql场景,我们全部都能用统一的方式进行。
   ⚠️ 写在末了:以上内容是我在学习以后得一些总结和概括,如有错误或者需要补充的地方接待各位大佬评论或者私信我交流!!!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表