更新一个表的字段值等于另一个表的字段值的SQL语句
将id相同的B表的name更新到A表
?
表a 字段id??? name????? 表b? id?? name
??????????? 1????? aa???????????????? 1??? aa
???????????? 2???? bb??????????????????2??? kk
??????????? 3????? cc???????????????????3?? mm
??????????? 4?????? ll
?
UPDATE a t
SET name = (SELECT name FROM b WHERE ID = t.ID)
WHERE EXISTS (SELECT 1 FROM b WHERE ID = t.ID AND name != t.name)
?
?
更新后A的结果
????????? 1????? aa?????????????????
??????????2??????kk????????????????
??????????3??????mm???????????????
????????? 4?????? ll
?