Home  >  Q&A  >  body text

MySQL,如何用一条sql来update两张表?

表A,字段1
表B,字段1
如何一条sql更新两张表?
设置字段1=100,字段2=200
update 表A,表B,set 表A.字段1......这样吗?

巴扎黑巴扎黑2742 days ago670

reply all(4)I'll reply

  • 迷茫

    迷茫2017-04-17 15:36:06

    UPDATE table1 AS a, table2 AS b, table3 AS c, ...
    SET a.name = 'W', b.name = 'T', c.name = 'F', ...
    WHERE a.id = 1 AND b.id = 2 AND c.id = 3

    However, it is not recommended to perform update operations on two tables with unrelated requirements in one SQL.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:36:06

    It is recommended to start a transaction to complete the update operation of the two tables.
    start transaction;
    update table A;
    update table B;
    commit;

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:36:06

    Why not give it a try?

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:36:06

    Such an amazing idea

    reply
    0
  • Cancelreply