MySQL 用其它表的值更新数据

备注:测试数据库版本为MySQL 8.0

如需要scott用户下建表及录入数据语句,可参考:
scott建表及录入数据sql脚本

测试数据准备:

mysql> create table new_sal(deptno int,sal int) character set utf8mb4;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into new_sal values (10,4000);
Query OK, 1 row affected (0.01 sec)

mysql> select * from new_sal;
+--------+------+
| deptno | sal  |
+--------+------+
|     10 | 4000 |
+--------+------+
1 row in set (0.00 sec)

问题
用用表new_sal中的值更新表emp中相应员工的工资,条件是emp.deptno与new_sal.deptno相等,将匹配记录的emp.sal更新为new_sla.sal,将emp.comm更新为new_sal.sal的50%。

解决方案:
在表new_sal和emp之间做连接,用来查找comm新值并带给update语句。像这样通过关联子查询进行更新的情况十分常见;另一种方法是创建视图(传统视图或内联视图,视数据库支持而定),然后更新这个视图。

update emp e
inner join new_sal ns
on e.deptno = ns.deptno
set e.sal = ns.sal,
    e.comm = ns.sal/2;

测试记录:

mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal     | comm    | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |    NULL |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 |    NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 |    NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-06-13 | 3000.00 |    NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 |    NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-06-13 | 1100.00 |    NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |    NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |    NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)

mysql> update emp e
    -> inner join new_sal ns
    -> on e.deptno = ns.deptno
    -> set e.sal = ns.sal,
    ->     e.comm = ns.sal/2;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> show warnings;
Empty set (0.00 sec)

mysql> 
mysql> select * from emp where deptno = 10;
+-------+--------+-----------+------+------------+---------+---------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal     | comm    | deptno |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 4000.00 | 2000.00 |     10 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 4000.00 | 2000.00 |     10 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 4000.00 | 2000.00 |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
3 rows in set (0.00 sec)
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页