您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL一条语句更新多个表的方法

2024/3/30 2:51:24发布5次查看
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语句更新多个表的,有时候这是非常有用的一个特性。
multiple-table syntax
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_table
table_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一条语句更新多个表的方法, 感谢原作者分享。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product