如何覆盖另一个 table 中的行?

How can I override rows from another table?

我有两个表:

表A

ID  Name
--  ----
1   aaa
2   bbb
3   ccc
4   ddd

表B

ID  Name
--  --------
3   WWXXYYZZ

我想从两个表中 select,但跳过表 B 中存在的行。结果应如下所示:

ID  Name
--  --------
1   aaa
2   bbb
3   WWXXYYZZ
4   ddd

我已经尝试了 unionjoin 但不知道如何实现。

-- Did not work
select *
from TableA
union
select *
from TableB

-- Did not work
select *
from
(
    select *
    from TableA
) x
join
(
    select *
    from TableB
) y
on x.ID = y.ID

一种方法是union all:

select b.*
from b
union all
select a.*
from a
where not exists (select 1 from a where a.id = b.id);

您也可以从 a 中选择并用 b:

中的值覆盖
select a.id, coalesce(b.name, a.name) as name
from a left join
     b
     on a.id = b.id;

您可以将 b 左连接到 a,然后使用 coalesce 优先选择 b 的行:

SELECT    a.id, COALESCE(b.name, a.name) AS name
FROM      a
LEFT JOIN b ON a.id = b.id

你可以这样做:

select a.id, coalesce(b.name, a.name)
from a left join b on a.id = b.id

一个更复杂的方法使用 ROW_NUMBER 如果您的查询比显示的要复杂得多,这可能是必要的。它还处理了 TableB 中存在一行而不是 TableA 的情况(您的问题不清楚)。

DECLARE @TableA TABLE (id INT, [Name] VARCHAR(12));
DECLARE @TableB TABLE (id INT, [Name] VARCHAR(12));

INSERT INTO @TableA (id, [Name])
VALUES
(1, 'aaa'),
(2, 'bbb'),
(3, 'ccc'),
(4, 'ddd');

INSERT INTO @TableB (id, [Name])
VALUES
(3, 'WWXXYYZZ'),
(5, 'TTTGGG');

SELECT id, [Name]
FROM (
    SELECT id, [Name]
        , ROW_NUMBER() OVER (PARTITION BY id ORDER BY [Priority] DESC) Rn
    FROM (
        SELECT id, [Name], 0 [Priority]
        FROM @TableA
        UNION ALL
        SELECT id, [Name], 1 [Priority]
        FROM @TableB
    ) X
  ) Y
WHERE Rn = 1;

Returns:

1   aaa
2   bbb
3   WWXXYYZZ
4   ddd
5   TTTGGG