就像用inner join这种连接写法可以一条sql更新两张表相关联的字段吗?

解决方案 »

  1.   

    mysql> create table ta(id int primary key,col int);
    Query OK, 0 rows affected (0.11 sec)mysql> create table tb(id int primary key,col int);
    Query OK, 0 rows affected (0.06 sec)mysql>
    mysql> insert into ta values (1,111);
    Query OK, 1 row affected (0.06 sec)mysql> insert into tb values (1,222);
    Query OK, 1 row affected (0.08 sec)mysql>
    mysql> update ta inner join tb on ta.id=tb.id
        -> set ta.col=100,tb.col=200;
    Query OK, 2 rows affected (0.06 sec)
    Rows matched: 2  Changed: 2  Warnings: 0mysql> select * from ta;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |  100 |
    +----+------+
    1 row in set (0.00 sec)mysql> select * from tb;
    +----+------+
    | id | col  |
    +----+------+
    |  1 |  200 |
    +----+------+
    1 row in set (0.02 sec)mysql>