多表更新

    最近在写SQL多表更新时遇到问题,网上找了找,几乎都是这样:

1.update  t1  set  t1.c2 =  t2.c2 
from   t2  where  t1.c1 =  t2.c1

2.update  t1  set  t1.c2  =  t2.c2
from  t1  inner  join  t2  on  t1.c1  = t2.c1

但是根本行不通,在MYSQL8.0版本的官网文档中查找时,只有关于将子查询作为派生表的做法:

UPDATE items, 
       (SELECT id FROM items 
        WHERE id IN 
            (SELECT id FROM items 
             WHERE retail / wholesale >= 1.3 AND quantity < 100)) 
        AS discounted 
SET items.retail = items.retail * 0.9 
WHERE items.id = discounted.id;

自己尝试了几次,MYSQL8.0版本的多表查询可以如此表示:

UPDATE t1,t2
SET t1.count = t1.count+1,t2.count = t2.count+1
WHERE t1.t1_id= t2.t2_id

可行。

你可能感兴趣的:(多表更新)