当 TableB 没有匹配条目时,仅使用 TableA 中的数据

Only use data from TableA when TableB has no matching entry

所以我有:

Table A with (not only Name - Multiple Columns):

| IDA        | Name        |
|:-----------|------------:|
| 1          |        John |
| 2          |       Jonas | 
| 3          |        Carl | 
| 4          |         Foo |
| 5          |         Bar |
| 6          |         Dog |

Table B with (not only Name - Multiple Columns):

| IDB        | Name        |
|:-----------|------------:|
| 1          |         Bob |
| 2          |        Lisa | 

Table 主要有:

| ID         | FKtoB       | FKtoA       |
|:-----------|------------:|------------:|
| 1          |           1 |           4 |
| 2          |             |           3 | 
| 3          |             |           1 | 
| 4          |           2 |           6 |
| 5          |             |           2 |
| 6          |             |           5 |

我的目标是从 MAIN 到 select,并且更喜欢使用 Table B 的数据,但如果不可用,我将使用 Table A 的数据。我尝试使用coalesce 但这没有帮助,因为我不想混合数据,所以如果例如 Table B 有一行数据,那么在这一行中应该没有来自 [=34 的数据=] A.

我的查询是这样的:

select 
coalesce(b.name, a.name) as surname, coalesce(b.surname, a.surname) as surname 
from MAIN m
left join TableA a on a.IDA = m.FKtoA
left join TableB b on b.IDA = m.FKtoB

我的问题是,如果例如 b.name 已填充,而 b.surname 不是,它会将 b.name 与 a.surname 混合,这对我来说是不行的.

尝试 Left JoinCase -

SELECT
    M.ID, (CASE WHEN B.IDB IS NOT NULL THEN B.Name ELSE A.Name END) AS NAME
FROM
    MAIN AS M
    LEFT JOIN TableB as B ON B.IDB = M.FKtoB
    LEFT JOIN TableA as A ON A.IDA = M.FKtoA

或许您可以添加 b 的 non-existens 作为连接条件:

select 
coalesce(b.name, a.name) as name, coalesce(b.surname, a.surname) as surname 
from MAIN m
left join TableB b on b.IDA = m.FKtoB
left join TableA a on a.IDA = m.FKtoA and b.IDA is null

编辑:将一个姓氏更正为姓名

从 SQL Server 2012(及更高版本)开始,您可以使用 IIF:

SELECT
IIF(m.FKtoB IS NULL, A.name, B.name) AS surname
FROM MAIN m
LEFT JOIN TableA a ON a.IDA = m.FKtoA
LEFT JOIN TableB b ON b.IDA = m.FKtoB

试试这个。它会给出你想要的结果。

(select ida as ID ,
case when ida is not null then ida end fkToIda,
 case when idb is not null then idb end fkToIdB 
 from A
left outer join B on a.ida=b.idb)

union

(select idb ,
case when ida is not null then ida end,
 case when idb is not null then idb end 
 from A
right outer join B on a.ida=b.idb)

试试这个:

    declare @tableA table(IDA INT,Name varchar(MAX))
    declare @tableB table(IDB INT, Name VARCHAR(MAX))
    declare @tableMain table(ID INT,FKtoB INT,FKtoA INT)

    INSERT INTO @tableA
    SELECT 1,'John'
    union all
    SELECT 2, 'Jonas'
    union all
    SELECT 3, 'Carl'
    union all
    SELECT 4,'Foo'
    union all
    SELECT 5 ,'Bar'
    union all
    SELECT 6,'Dog'


    INSERT INTO @tableB
    SELECT 1, 'Bob'
    Union all
    SELECT 2, 'Lisa'


    INSERT INTO @tableMain
    SELECT 1,1,4
    union all
    SELECT 2,null,3
    union all
    SELECT 3,null,1
    union all
    SELECT 4,2,6
    union all
    SELECT 5,null,2
    union all
    SELECT 6,null,5


    Select tm.Id,ISNULL(tb.Name,ta.Name) As NAME from @tableMain tm
    LEFT JOIN @tableB tb on tm.FktoB=tb.IDb and IDb is not NUll
    LEFT JOIN @tableA tA on tm.FktoA=ta.IDA and FktoB is NUll

你想 select 从 table A 只有当在 table B 中没有匹配时,所以给定的外键是完整的:

select 
  coalesce(b.firstname, a.firstname) as firstname,
  coalesce(b.surname, a.surname) as surname,
  coalesce(b.birthday, a.birthday) as birthday,
  ...
from MAIN m
left join TableB b on b.IDB = m.FKtoB
left join TableA a on a.IDA = m.FKtoA and m.FKtoB is null;

另一种方法是联合所有:

select firstname, surname, birthday, ...
from TableB 
where IDB in (select FKtoB from MAIN)
union all
select firstname, surname, birthday, ...
from TableA 
where IDA in (select FKtoA from MAIN where FKtoB is null);