我有2个表格,其日期字段与该需求不匹配。
首先,我有一个查询来返回所有需要更新日期字段的记录列表。
SELECT distinct ID_NUMBER
FROM a
INNER JOIN b
ON a.ID_NUMBER = b.ID_NUMBER
INNER JOIN c
ON c.ID_NUMBER = b.ID_NUMBER
INNER JOIN d
ON c.ID_NUMBER = d.ID_NUMBER
WHERE d.DATE <> b.DATE
返回20K加上ID_NUMBER的
我想更新表B,其中ID_Number =来自该查询的ID_numbers列表。
尝试:
UPDATE b
SET d.DATE = b.DATE
Where (select distinct ID_NUMBER
from a
INNER JOIN b
ON a.ID_NUMBER = b.ID_NUMBER
INNER JOIN c
ON c.ID_NUMBER = b.ID_NUMBER
INNER JOIN d
ON c.ID_NUMBER = d.ID_NUMBER
Where d.DATE <> b.DATE
)
应该是直截了当的。似乎无法获得语法。帮助赞赏。
答案 0 :(得分:1)
因为他们都有相同的ID_NUMBER,你只是看着表b和d,你不能这样做吗?
UPDATE b
SET b.DATE = d.DATE
FROM b INNER JOIN d ON b.ID_NUMBER = d.ID_NUMBER
WHERE d.DATE <> b.DATE
AND d.ID_NUMBER = b.ID_NUMBER;
SQL小提琴链接:http://sqlfiddle.com/#!3/a3af9/3
答案 1 :(得分:0)
我能够使这个更新查询工作。您可以使用SQL Fiddle上的一些示例数据查看它。
UPDATE b
SET b.DATE = d.DATE
FROM a
INNER JOIN b ON a.ID_NUMBER = b.ID_NUMBER
INNER JOIN c ON c.ID_NUMBER = b.ID_NUMBER
INNER JOIN d ON c.ID_NUMBER = d.ID_NUMBER
WHERE d.DATE <> b.DATE
注意:这与@ Sonam的答案类似,但SET
子句需要设置b.DATE
列而不是d.DATE
列,因为表b
正在更新
答案 2 :(得分:-1)
UPDATE b
SET d.DATE = b.DATE
from a INNER JOIN b
ON a.ID_NUMBER = b.ID_NUMBER
INNER JOIN c ON c.ID_NUMBER = b.ID_NUMBER
INNER JOIN d ON c.ID_NUMBER = d.ID_NUMBER
Where d.DATE <> b.DATE