mysql 可以基于多表查询更新数据。对于多表的 update 操作需要慎重,建议在更新前,先使用 select 语句查询验证更新的数据与自己期望的是否一致。
下面我们建两张表,一张表为 product 表,用来存放产品信息,其中有产品价格字段 price;另外一张表是 product_price 表。现要将 product_price 表中的价格字段 price 更新为 product 表中价格字段 price 的 80%。
操作前先分别查看两张表的数据,sql 语句和运行结果如下:
mysql> select * from product; +----+-----------+-----------------------+-------+----------+ | id | productid | productname | price | isdelete | +----+-----------+-----------------------+-------+----------+ | 1 | 1001 | c语言中文网java教程 | 100 | 0 | | 2 | 1002 | c语言中文网mysql教程 | 110 | 0 | | 3 | 1003 | c语言中文网python教程 | 120 | 0 | | 4 | 1004 | c语言中文网c语言教程 | 150 | 0 | | 5 | 1005 | c语言中文网golang教程 | 160 | 0 | +----+-----------+-----------------------+-------+----------+ 5 rows in set (0.02 sec) mysql> select * from product_price; +----+-----------+-------+ | id | productid | price | +----+-----------+-------+ | 1 | 1001 | null | | 2 | 1002 | null | | 3 | 1003 | null | | 4 | 1004 | null | | 5 | 1005 | null | +----+-----------+-------+ 5 rows in set (0.01 sec)
下面是 mysql 多表更新在实践中的几种不同写法。执行不同的 sql 语句,仔细观察 sql 语句执行后表中数据的变化,很容易就能理解多表联合更新的用法。
1. 使用update
在 mysql 中,可以使用“update table1 t1,table2,...,table n”的方式来多表更新,sql 语句和运行结果如下:
mysql> update product p, product_price pp set pp.price = p.price * 0.8 where p.productid= pp.productid; query ok, 5 rows affected (0.02 sec) rows matched: 5 changed: 5 warnings: 0 mysql> select * from product_price; +----+-----------+-------+ | id | productid | price | +----+-----------+-------+ | 1 | 1001 | 80 | | 2 | 1002 | 88 | | 3 | 1003 | 96 | | 4 | 1004 | 120 | | 5 | 1005 | 128 | +----+-----------+-------+ 5 rows in set (0.00 sec)
2. 通过inner join
另外一种方法是使用 inner join 来多表更新。sql 语句如下:
mysql> update product p inner join product_price pp on p.productid= pp.productid set pp.price = p.price * 0.8; query ok, 5 rows affected (0.09 sec) rows matched: 5 changed: 5 warnings: 0 mysql> select * from product_price; +----+-----------+-------+ | id | productid | price | +----+-----------+-------+ | 1 | 1001 | 80 | | 2 | 1002 | 88 | | 3 | 1003 | 96 | | 4 | 1004 | 120 | | 5 | 1005 | 128 | +----+-----------+-------+ 5 rows in set (0.00 sec)
3. 通过left join
也可以使用 left join 来做多表更新,如果 product_price 表中没有产品价格记录的话,将 product 表的 isdelete 字段设置为 1。在 product 表添加 1006 商品,且不在 product_price 表中添加对应信息,sql 语句如下。
mysql> update product p left join product_price pp on p.productid= pp.productid set p.isdelete = 1 where pp.productid is null; query ok, 1 row affected (0.04 sec) rows matched: 1 changed: 1 warnings: 0 mysql> select * from product; +----+-----------+-----------------------+-------+----------+ | id | productid | productname | price | isdelete | +----+-----------+-----------------------+-------+----------+ | 1 | 1001 | c语言中文网java教程 | 100 | 0 | | 2 | 1002 | c语言中文网mysql教程 | 110 | 0 | | 3 | 1003 | c语言中文网python教程 | 120 | 0 | | 4 | 1004 | c语言中文网c语言教程 | 150 | 0 | | 5 | 1005 | c语言中文网golang教程 | 160 | 0 | | 6 | 1006 | c语言中文网spring教程 | null | 1 | +----+-----------+-----------------------+-------+----------+ 6 rows in set (0.00 sec)
4. 通过子查询
也可以通过子查询进行多表更新,sql 语句和执行过程如下:
mysql> update product_price pp set price=(select price*0.8 from product where productid = pp.productid); query ok, 5 rows affected (0.00 sec) rows matched: 5 changed: 5 warnings: 0 mysql> select * from product_price; +----+-----------+-------+ | id | productid | price | +----+-----------+-------+ | 1 | 1001 | 80 | | 2 | 1002 | 88 | | 3 | 1003 | 96 | | 4 | 1004 | 120 | | 5 | 1005 | 128 | +----+-----------+-------+ 5 rows in set (0.00 sec)
另外,上面的几个例子都是在两张表之间做关联,只更新一张表中的记录。mysql 也可以同时更新两张表,如下语句就同时修改了两个表。
update product p inner join product_price pp on p.productid= pp.productid set pp.price = p.price * 0.8, p.dateupdate = curdate()
两张表做关联,同时更新了 product_price 表的 price 字段和 product 表的 dateupdate 两个字段。
日常开发中,一般都是用单表 update 语句,很少写多表关联的 update。
到此这篇关于mysql 多表联合更新的项目实践的文章就介绍到这了,更多相关mysql 多表联合更新内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论