在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.MySQL UPDATE JOIN语法在MySQL中,可以在 UPDATE语句 中使用 UPDATE T1, T2, [INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2. C1 SET T1.C2 = T2.C2, T2.C3 = expr WHERE condition 更详细地看看MySQL 首先,在 第二,指定一种要使用的连接,即 第三,要为要更新的 第四,WHERE子句中的条件用于指定要更新的行。 2. 示例首先,我们将在这些例子中使用一个新的示例数据库(
以下语句在 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示例 假设想根据员工的工作表现来调整员工的工资。 上面查询语句的工作原理是什么? 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 employees INNER JOIN merits ON employees.performance = merits.performance SET salary = salary + salary * percentage WHERE employees.performance > 1; 2.2使用LEFT JOIN的MySQL UPDATE JOIN示例 假设公司又雇用了两名新员工: INSERT INTO employees(emp_name,performance,salary) VALUES('Jack William',NULL,43000), ('Ricky Bond',NULL,52000); 因为这些员工是新员工,所以他们的绩效( 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 employees LEFT JOIN merits ON employees.performance = merits.performance SET salary = salary + salary * 0.015 WHERE merits.percentage IS NULL; 执行结果如下: 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 示例 # 单表join update bbs_uhome_card_activate ca INNER JOIN bbs_uhome_card_rules cr on ca.card_brach=cr.card_bach set ca.create_user=cr.create_user; # 多表join UPDATE bbs_uhome_card_order co INNER JOIN bbs_uhome_card_order_record cor on co.order_no=cor.order_no JOIN bbs_uhome_card_activate ca on cor.card_no=ca.card_no set co.create_user=ca.create_user 到此这篇关于Mysql联表update数据的示例详解的文章就介绍到这了,更多相关Mysql联表update数据内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论