2017-09-30 43 views
1

我有2个表选择中间表中与给定值/值相匹配的记录以及不在中间表中的记录?

CREATE TABLE Floor (
Id bigint primary key identity(1,1), 
Name 
) 

表分配充当中介桌到桌楼

CREATE TABLE Assignment(
Id bigint primary key identity(1,1), 
AccountId bigint, 
. 
. 
FloorId bigint, --foreign key for Id column of floor table 
. 
. 
) 

如何选择:

  1. 从地板表记录中的其中包含一个XML与Assignment表中的accountId匹配的accountId。

鉴于:

DECLARE @accountIdsXML XML = '<AccountId>73</AccountId><AccountId>74</AccountId>' 
  • 记录不具有匹配在分配表
  • 实施例:

    表值:

    Floor Table 
    Id Name 
    1  1st Floor 
    2  2nd Floor 
    3  3rd Floor 
    4  4th Floor 
    5  5th Floor 
    6  6th Floor 
    7  7th Floor 
    8  8th Floor 
    9  9th Floor 
    10 10th Floor 
    
    Assignment Table 
    Id AccountId FloorId 
    1  73   1 
    1  73   2 
    1  76   3 
    1  79   4 
    1  74   5 
    1  74   6 
    1  79   7 
    

    选择输出:

    Floor table 
    Id Name 
    1  1st Floor --matches accountid 73 
    2  2nd Floor --matches accountid 73 
    5  5th Floor --matches accountid 74 
    6  6th Floor --matches accountid 74 
    8  8th Floor --not in assignment table 
    9  9th Floor --not in assignment table 
    10 10th Floor --not in assignment table 
    

    我基本上要选择分配给定ACCOUNTID和未分配的地板地板。

    我已经成功地做到

    DECLARE @accountIdsXML XML = '<AccountId>73</AccountId><AccountId>74</AccountId>' 
    DECLARE @accountIdsTable as TABLE (Id BIGINT) --table variable to store accountids to be used in the where in operator. 
    
    --insert accountids to the table variable 
    INSERT INTO @accountIdsTable 
    SELECT [aid].[Col].value('(.)[1]', 'BIGINT') FROM @accountIdsXML.nodes('/AccountId') as [aid]([Col]) 
    
    SELECT F.* FROM [Assignment] A RIGHT JOIN [Floor] F ON A.FloorId = F.Id 
    WHERE (A.AccountId IN(SELECT Id from @accountIdsTable) OR F.Id NOT IN (SELECT FloorId FROM Assignment)) 
    

    回答

    1

    不要从分配选择,而是只从楼层选择,然后在第一个WHERE子句你加入他们两个:

    SELECT F.* FROM [Floor] F 
    WHERE EXISTS 
    (
        SELECT 1 FROM [Assignment] A 
        INNER JOIN @accountIdsTable AI ON AI.Id = A.AccountId  
        WHERE A.FloorId = F.Id 
    ) 
    OR F.Id NOT IN (SELECT FloorId FROM Assignment) 
    

    这里的a link the Fiddle

    +0

    工作@ Gabriel Rainha,tnx –

    相关问题