原文地址:https://www.jianshu.com/p/f99665266bb1html
在本教程中,您將學習如何使用MySQL UPDATE JOIN
語句來執行跨表更新。咱們將逐步介紹如何使用INNER JOIN
子句和LEFT JOIN
子句與UPDATE
語句一塊兒使用。mysql
咱們常常使用join
子句來查詢表中的行(在INNER JOIN的狀況下),或者可能沒有(在LEFT JOIN的狀況下)另外一個表中的相應行。 在MySQL中,能夠在UPDATE語句中使用JOIN
子句執行跨表更新。sql
MySQL UPDATE JOIN
的語法以下:數據庫
UPDATE T1, T2, [INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2. C1 SET T1.C2 = T2.C2, T2.C3 = expr WHERE condition
讓咱們更詳細地看看MySQL UPDATE JOIN
語法:yii
UPDATE
子句以後,指定主表(T1
)和但願主錶鏈接表(T2
)。 請注意,必須在UPDATE
子句以後至少指定一個表。UPDATE
子句後未指定的表中的數據未更新。INNER JOIN
或LEFT JOIN
和鏈接條件。JOIN
子句必須出如今UPDATE
子句以後。T1
和/或T2
表中的列分配新值。若是您學習過了UPDATE語句教程,您可能會注意到使用如下語法更新數據交叉表的另外一種方法:學習
UPDATE T1, T2 SET T1.c2 = T2.c2, T2.c3 = expr WHERE T1.c1 = T2.c1 AND condition
在這個UPDATE
語句與具備隱式INNER JOIN
子句的UPDATE JOIN
工做相同。這意味着能夠以下重寫上述語句:spa
UPDATE T1,T2 INNER JOIN T2 ON T1.C1 = T2.C1 SET T1.C2 = T2.C2, T2.C3 = expr WHERE condition
讓咱們來看一些使用UPDATE JOIN
語句來更好地理解的例子。code
咱們將在這些例子中使用一個新的示例數據庫(empdb)。示例數據庫包含2
個表:orm
employees
表將存儲在員工編號,姓名,工做表現和工資的數據。merits
表存儲員工績效和績效百分比。如下語句在empdb
示例數據庫中建立表並導入數據:htm
CREATE DATABASE IF NOT EXISTS empdb; USE empdb; -- create tables CREATE TABLE merits ( performance INT(11) NOT NULL, percentage FLOAT NOT NULL, PRIMARY KEY (performance) ); CREATE TABLE employees ( emp_id INT(11) NOT NULL AUTO_INCREMENT, emp_name VARCHAR(255) NOT NULL, performance INT(11) DEFAULT NULL, salary FLOAT DEFAULT NULL, PRIMARY KEY (emp_id), CONSTRAINT fk_performance FOREIGN KEY (performance) REFERENCES merits (performance) ); -- insert data for merits table INSERT INTO merits(performance,percentage) VALUES(1,0), (2,0.01), (3,0.03), (4,0.05), (5,0.08); -- insert data for employees table INSERT INTO employees(emp_name,performance,salary) VALUES('Mary Doe', 1, 50000), ('Cindy Minsu', 3, 65000), ('Sue Greenspan', 4, 75000), ('Grace Dell', 5, 125000), ('Nancy Johnson', 3, 85000), ('John Doe', 2, 45000), ('Lily Bush', 3, 55000);
2.1 使用INNER JOIN子句的MySQL UPDATE JOIN示例
假設想根據員工的工做表現來調整員工的工資。
所以,優勢百分比存儲在merits
表中,您必須使用UPDATE INNER JOIN
語句根據存儲在merits
表中的百分比來調整employees
表中員工的工資。
employees
和merits
表之間以是performance
字段相關聯的。 請參閱如下查詢:
UPDATE employees INNER JOIN merits ON employees.performance = merits.performance SET salary = salary + salary * percentage;
上面查詢語句的工做原理是什麼?
咱們僅在UPDATE
子句以後指定employees
表,由於咱們只想更新employees
表中的數據。
對於employees
表中的每一行,查詢根據merits
表中performance
列中的值來檢查employees
表中的performance
列中的值。 若是找到一個匹配,它將得到merits
表中的百分比,並更新employees
表中的salary
列。
mysql> select * from employees; -- 更新以前的數據 +--------+---------------+-------------+--------+ | emp_id | emp_name | performance | salary | +--------+---------------+-------------+--------+ | 1 | Mary Doe | 1 | 50000 | | 2 | Cindy Minsu | 3 | 65000 | | 3 | Sue Greenspan | 4 | 75000 | | 4 | Grace Dell | 5 | 125000 | | 5 | Nancy Johnson | 3 | 85000 | | 6 | John Doe | 2 | 45000 | | 7 | Lily Bush | 3 | 55000 | +--------+---------------+-------------+--------+ 7 rows in set mysql> UPDATE employees INNER JOIN merits ON employees.performance = merits.performance SET salary = salary + salary * percentage; -- 執行鏈接更新 Query OK, 6 rows affected Rows matched: 7 Changed: 6 Warnings: 0 mysql> select * from employees; -- 更新以後的數據 +--------+---------------+-------------+--------+ | emp_id | emp_name | performance | salary | +--------+---------------+-------------+--------+ | 1 | Mary Doe | 1 | 50000 | | 2 | Cindy Minsu | 3 | 66950 | | 3 | Sue Greenspan | 4 | 78750 | | 4 | Grace Dell | 5 | 135000 | | 5 | Nancy Johnson | 3 | 87550 | | 6 | John Doe | 2 | 45450 | | 7 | Lily Bush | 3 | 56650 | +--------+---------------+-------------+--------+ 7 rows in set
由於省略了UPDATE
語句中的WHERE
子句,因此employees
表中的全部記錄都被更新。
2.2 具備LEFT JOIN的MySQL UPDATE JOIN示例
假設公司又僱用了兩名新員工:
INSERT INTO employees(emp_name,performance,salary) VALUES('Jack William',NULL,43000), ('Ricky Bond',NULL,52000);
由於這些員工是新員工,因此他們的績效(performance
)數據不可用或爲NULL
。如今 employees
表中的數據,以下所示 -
mysql> SELECT * FROM employees; +--------+---------------+-------------+--------+ | emp_id | emp_name | performance | salary | +--------+---------------+-------------+--------+ | 1 | Mary Doe | 1 | 50000 | | 2 | Cindy Minsu | 3 | 66950 | | 3 | Sue Greenspan | 4 | 78750 | | 4 | Grace Dell | 5 | 135000 | | 5 | Nancy Johnson | 3 | 87550 | | 6 | John Doe | 2 | 45450 | | 7 | Lily Bush | 3 | 56650 | | 8 | Jack William | NULL | 43000 | | 9 | Ricky Bond | NULL | 52000 | +--------+---------------+-------------+--------+ 9 rows in set
要計算新員工的工資,不能使用UPDATE INNER JOIN
語句,由於它們的績效數據在merits
表中不可用。這就是爲何要使用UPDATE LEFT JOIN
來實現了。
當UPDATE LEFT JOIN
語句在另外一個表中沒有相應行時,就會更新表中的一行。
例如,您能夠使用如下語句將新僱員的工資增長1.5%
:
UPDATE employees LEFT JOIN merits ON employees.performance = merits.performance SET salary = salary + salary * 0.015 WHERE merits.percentage IS NULL;
上面語句執行後,獲得如下結果 -
mysql> SELECT * FROM employees; +--------+---------------+-------------+--------+ | emp_id | emp_name | performance | salary | +--------+---------------+-------------+--------+ | 1 | Mary Doe | 1 | 50000 | | 2 | Cindy Minsu | 3 | 66950 | | 3 | Sue Greenspan | 4 | 78750 | | 4 | Grace Dell | 5 | 135000 | | 5 | Nancy Johnson | 3 | 87550 | | 6 | John Doe | 2 | 45450 | | 7 | Lily Bush | 3 | 56650 | | 8 | Jack William | NULL | 43000 | | 9 | Ricky Bond | NULL | 52000 | +--------+---------------+-------------+--------+ 9 rows in set mysql> UPDATE employees LEFT JOIN merits ON employees.performance = merits.performance SET salary = salary + salary * 0.015 WHERE merits.percentage IS NULL; Query OK, 2 rows affected Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from employees; +--------+---------------+-------------+--------+ | emp_id | emp_name | performance | salary | +--------+---------------+-------------+--------+ | 1 | Mary Doe | 1 | 50000 | | 2 | Cindy Minsu | 3 | 66950 | | 3 | Sue Greenspan | 4 | 78750 | | 4 | Grace Dell | 5 | 135000 | | 5 | Nancy Johnson | 3 | 87550 | | 6 | John Doe | 2 | 45450 | | 7 | Lily Bush | 3 | 56650 | | 8 | Jack William | NULL | 43645 | | 9 | Ricky Bond | NULL | 52780 | +--------+---------------+-------------+--------+ 9 rows in set
在本教程中,咱們向您展現瞭如何使用MySQL UPDATE JOIN
與INNER JOIN
和LEFT JOIN
子句來執行跨表更新。