MySQL本身是支持一条update语句更新多个表的,有时候这是非常有用的一个特性。 Multiple-table syntax UPDATE [LOW_PRIORITY] [IGNORE] table_references SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] [WHERE where_condition]/pre 于是继
MySQL本身是支持一条update语句更新多个表的,有时候这是非常有用的一个特性。
UPDATE [LOW_PRIORITY] [IGNORE] table_references SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] … [WHERE where_condition]
于是继续找table_references说明;
table_references:
escaped_table_reference [, escaped_table_reference] …escaped_table_reference:
table_reference
| { OJ table_reference }table_reference:
table_factor
| join_tabletable_factor:
tbl_name [[AS] alias] [index_hint]
| table_subquery [AS] alias
| ( table_references )
可以看到,update的关键词可以写多个表,每个表也可以是个子查询、也可以是join语句。
在我的另一篇文章中,我已经用到了该语法:
UPDATE table_a,table_b SET table_a.age=table_b.age WHERE table_a.id=table_b.id;
该语句中的table_b表也可以换成子查询、join子句,比如:
UPDATE table_a,(SELECT id,age FROM table_b) AS tb SET table_a.age=tb.age WHERE table_a.id=tb.id;
mysql update官方文档:http://dev.mysql.com/doc/refman/5.0/en/update.html
转载请注明来源:链接
原文地址:MySQL一条语句更新多个表的方法, 感谢原作者分享。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号