在SQL Server中如何从一个表中获取不匹配的记录[关闭]

问题描述 投票:-3回答:3

我在SQL Server中有两个具有父子关系的表。我想在父表数据的帮助下使用join更新子表记录的数据

sql sql-server
3个回答
1
投票

您可以尝试以下内容。如果您需要更具体的帮助,那么您需要提供更多详细信息:

SELECT a.*
FROM a
LEFT OUTER JOIN b
ON a.col = b.col
WHERE b.col IS NULL;

0
投票

下面你可以更新子表

   UPDATE c
        SET c.id=p.id        
    FROM Parent p
    INNER JOIN child c
        ON c.Id = p.Id

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
© www.soinside.com 2019 - 2024. All rights reserved.