我在SQL Server中有两个具有父子关系的表。我想借助join使用父表数据来更新子表记录的数据
答案 0 :(得分:1)
您可以尝试以下操作。如果您需要更具体的帮助,则需要提供更多详细信息:
SELECT a.*
FROM a
LEFT OUTER JOIN b
ON a.col = b.col
WHERE b.col IS NULL;
答案 1 :(得分:0)
以下更新子表的方式
UPDATE c
SET c.id=p.id
FROM Parent p
INNER JOIN child c
ON c.Id = p.Id
答案 2 :(得分:0)
根据您的问题,我将考虑不同的情况:
特殊情况:
select c.id
from child c left join parent p on p.id = c.parent_id
where c.parent_id is null -- have to manually update
select c.id
from child c left join parent p on p.id <> c.parent_id
where c.parent_id not in (select id from parent) -- orphan records
标准案例:
select c.*
from child c left join parent p on p.id = c.parent_id and p.info <> c.parent_info
update c
set c.parent_info = p.info
from child c left join parent p on p.id = c.parent_id and p.info <> c.parent_info