-3

我有 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
)

应该是直截了当的。似乎无法获得语法。帮助表示赞赏。

4

3 回答 3

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 Fiddle 链接:http ://sqlfiddle.com/#!3/a3af9/3

于 2013-08-30T18:27:21.210 回答
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正在更新表。

于 2013-08-30T18:07:26.313 回答
-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
于 2013-08-30T17:53:16.317 回答