我有两个表:
表1:
Customer employee
ASD_1234 WF001
ASD_1235 WF002
ASD_1236 WF003
ASD_1237 NULL
ASD_1238 NULL
ASD_1239 NULL
ASD_1240 WF004
ASD_1234 WF001
ASD_1236 WF003
ASD_1240 WF004
表2:
Customer com_employee
ASD_1234 WF001
ASD_1235 WF002
ASD_1236 WF003
ASD_1237 WF005
ASD_1238 WF006
ASD_1239 WF007
ASD_1240 WF004
表2是由唯一的 Customer 组成的元数据。现在,我需要通过在Customer上加入table2来更新表1中的空值。我该如何实现? 谢谢
答案 0 :(得分:1)
您可以使用相关查询:
UPDATE table1
SET employee = (
SELECT com_employee
FROM table2
WHERE table2.customer = table1.customer
)
WHERE employee IS NULL
答案 1 :(得分:0)
您可以使用带有employee
无效性的条件的update-join语句:
UPDATE t1
SET t1.employee = t2.com_employee
FROM t1
JOIN t2 ON t1.customer = t2.customer
WHERE t1.employee IS NULL
答案 2 :(得分:0)
我们可以在此处尝试使用可更新的CTE:
WITH cte AS (
SELECT t1.employee, t2.com_employee
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Customer = t2.Customer
)
UPDATE cte
SET employee = com_employee
WHERE employee IS NULL;