MySQL利用update实现多表更新

编程入门 行业动态 更新时间:2024-10-08 20:38:28

<a href=https://www.elefans.com/category/jswz/34/1771279.html style=MySQL利用update实现多表更新"/>

MySQL利用update实现多表更新

MySQL利用update实现多表更新

一、数据准备

创建stu(学生)表和certificate(证书)表,并输入数据,代码如下:

create table stu(stu_id int primary key,stu_name char(20) not null default '',certificate_no char(20) not null default ''
);
insert into stu(stu_id,stu_name) values(10001,'张晓云'),(10002,'王云飞'),
(10003,'李大鹏'),(10004,'王大刚'),(10005,'张小倩'),(10006,'刘明明'),
(10007,'刘涛'),(10008,'张华'),(10009,'陈冰'),(10010,'张静静');create table certificate(stu_id int primary key,certificate_type char(50) not null default '',certificate_no char(20) not null default '',certificate_date datetime
);insert into certificate(stu_id,certificate_type,certificate_no,certificate_date) 
values(10001,'计算机二级office高级应用','Comp-2-25879','2018-3-15'),
(10002,'计算机三级网络技术','Comp-3-25666','2018-9-28'),
(10003,'英语四级','CET-4-12458','2018-12-25'),
(10005,'英语四级','CET-4-25487','2018-12-25'),
(10007,'英语六级','CET-6-42156','2019-6-28'),
(10008,'英语六级','CET-6-37455','2019-6-28');

如果stu表的学生考取的有证书,则把certificate表中的certificate_no填入stu表。

二、使用join关键字建立连接

mysql> update stu s inner join certificate c on s.stu_id=c.stu_idset s.certificate_no=c.certificate_no;
Query OK, 6 rows affected (0.01 sec)
Rows matched: 6  Changed: 6  Warnings: 0mysql> select * from stu;
+--------+-----------+----------------+
| stu_id | stu_name  | certificate_no |
+--------+-----------+----------------+
|  10001 | 张晓云    | Comp-2-25879   |
|  10002 | 王云飞    | Comp-3-25666   |
|  10003 | 李大鹏    | CET-4-12458    |
|  10004 | 王大刚    |                |
|  10005 | 张小倩    | CET-4-25487    |
|  10006 | 刘明明    |                |
|  10007 | 刘涛      | CET-6-42156    |
|  10008 | 张华      | CET-6-37455    |
|  10009 | 陈冰      |                |
|  10010 | 张静静    |                |
+--------+-----------+----------------+
10 rows in set (0.00 sec)

三、不使用join关键字建立连接

mysql> update stu set certificate_no='';
Query OK, 6 rows affected (0.01 sec)
Rows matched: 10  Changed: 6  Warnings: 0mysql> select * from stu;
+--------+-----------+----------------+
| stu_id | stu_name  | certificate_no |
+--------+-----------+----------------+
|  10001 | 张晓云    |                |
|  10002 | 王云飞    |                |
|  10003 | 李大鹏    |                |
|  10004 | 王大刚    |                |
|  10005 | 张小倩    |                |
|  10006 | 刘明明    |                |
|  10007 | 刘涛      |                |
|  10008 | 张华      |                |
|  10009 | 陈冰      |                |
|  10010 | 张静静    |                |
+--------+-----------+----------------+
10 rows in set (0.01 sec)mysql> update stu s,certificate cset s.certificate_no=c.certificate_nowhere s.stu_id=c.stu_id;
Query OK, 6 rows affected (0.01 sec)
Rows matched: 6  Changed: 6  Warnings: 0mysql> select * from stu;
+--------+-----------+----------------+
| stu_id | stu_name  | certificate_no |
+--------+-----------+----------------+
|  10001 | 张晓云    | Comp-2-25879   |
|  10002 | 王云飞    | Comp-3-25666   |
|  10003 | 李大鹏    | CET-4-12458    |
|  10004 | 王大刚    |                |
|  10005 | 张小倩    | CET-4-25487    |
|  10006 | 刘明明    |                |
|  10007 | 刘涛      | CET-6-42156    |
|  10008 | 张华      | CET-6-37455    |
|  10009 | 陈冰      |                |
|  10010 | 张静静    |                |
+--------+-----------+----------------+
10 rows in set (0.00 sec)

四、使用子查询

mysql> update stu set certificate_no='';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 6  Changed: 4  Warnings: 0mysql> select * from stu;
+--------+-----------+----------------+
| stu_id | stu_name  | certificate_no |
+--------+-----------+----------------+
|  10001 | 张晓云    |                |
|  10002 | 王云飞    |                |
|  10003 | 李大鹏    |                |
|  10004 | 王大刚    |                |
|  10005 | 张小倩    |                |
|  10006 | 刘明明    |                |
|  10007 | 刘涛      |                |
|  10008 | 张华      |                |
|  10009 | 陈冰      |                |
|  10010 | 张静静    |                |
+--------+-----------+----------------+
10 rows in set (0.01 sec)mysql> update stu set stu.certificate_no=(select certificate.certificate_no from certificate where certificate.stu_id=stu.stu_id)where exists (select 1 from certificate where certificate.stu_id=stu.stu_id);
Query OK, 0 rows affected (0.01 sec)
Rows matched: 6  Changed: 0  Warnings: 0mysql> select * from stu;
+--------+-----------+----------------+
| stu_id | stu_name  | certificate_no |
+--------+-----------+----------------+
|  10001 | 张晓云    | Comp-2-25879   |
|  10002 | 王云飞    | Comp-3-25666   |
|  10003 | 李大鹏    | CET-4-12458    |
|  10004 | 王大刚    |                |
|  10005 | 张小倩    | CET-4-25487    |
|  10006 | 刘明明    |                |
|  10007 | 刘涛      | CET-6-42156    |
|  10008 | 张华      | CET-6-37455    |
|  10009 | 陈冰      |                |
|  10010 | 张静静    |                |
+--------+-----------+----------------+
10 rows in set (0.00 sec)

五、同时更新两个表中的数据

mysql> update stu set certificate_no='';
Query OK, 6 rows affected (0.01 sec)
Rows matched: 10  Changed: 6  Warnings: 0mysql> select * from stu;
+--------+-----------+----------------+
| stu_id | stu_name  | certificate_no |
+--------+-----------+----------------+
|  10001 | 张晓云    |                |
|  10002 | 王云飞    |                |
|  10003 | 李大鹏    |                |
|  10004 | 王大刚    |                |
|  10005 | 张小倩    |                |
|  10006 | 刘明明    |                |
|  10007 | 刘涛      |                |
|  10008 | 张华      |                |
|  10009 | 陈冰      |                |
|  10010 | 张静静    |                |
+--------+-----------+----------------+
10 rows in set (0.01 sec)mysql> update stu s inner join certificate c on s.stu_id=c.stu_idset s.certificate_no=c.certificate_no, c.certificate_date='2019-8-8'where certificate_type='英语六级';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0mysql> select * from stu;
+--------+-----------+----------------+
| stu_id | stu_name  | certificate_no |
+--------+-----------+----------------+
|  10001 | 张晓云    |                |
|  10002 | 王云飞    |                |
|  10003 | 李大鹏    |                |
|  10004 | 王大刚    |                |
|  10005 | 张小倩    |                |
|  10006 | 刘明明    |                |
|  10007 | 刘涛      | CET-6-42156    |
|  10008 | 张华      | CET-6-37455    |
|  10009 | 陈冰      |                |
|  10010 | 张静静    |                |
+--------+-----------+----------------+
10 rows in set (0.00 sec)mysql> select * from certificate;
+--------+-----------------------------------+----------------+---------------------+
| stu_id | certificate_type                  | certificate_no | certificate_date    |
+--------+-----------------------------------+----------------+---------------------+
|  10001 | 计算机二级office高级应用          | Comp-2-25879   | 2018-03-15 00:00:00 |
|  10002 | 计算机三级网络技术                | Comp-3-25666   | 2018-09-28 00:00:00 |
|  10003 | 英语四级                          | CET-4-12458    | 2018-12-25 00:00:00 |
|  10005 | 英语四级                          | CET-4-25487    | 2018-12-25 00:00:00 |
|  10007 | 英语六级                          | CET-6-42156    | 2019-08-08 00:00:00 |
|  10008 | 英语六级                          | CET-6-37455    | 2019-08-08 00:00:00 |
+--------+-----------------------------------+----------------+---------------------+
6 rows in set (0.00 sec)

更多推荐

MySQL利用update实现多表更新

本文发布于:2024-02-13 08:47:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL   update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!