如果字段不相等,则从另一个表更新mysql

时间:2016-12-05 03:43:50

标签: mysql

这个查询有什么问题?

update TA,TB
set TA.change = 1,TA.value = TB.value
where
TA.name  = TB.name and TA.value <> TB.value

我想在结核病价值变化时从TB更新TA

1 个答案:

答案 0 :(得分:0)

您的查询没问题。我会用明确的join编写它:

update TA join
       TB
       on TA.name  = TB.name
     set TA.change = 1,
         TA.value = TB.value
where TA.value <> TB.value;

您可能需要考虑NULL个值:

update TA join
       TB
       on TA.name  = TB.name
     set TA.change = 1,
         TA.value = TB.value
where not (TA.value <=> TB.value);