内连接返回两个表中的所有值

时间:2017-09-20 01:43:33

标签: sql sql-server join sql-server-2012

我有两个表,TableA和TableB,

TableA
-------------------------------------------------
Date        ORDERNUMBER PARTNUMBER  ID
--------------------------------------------------------
2017-08-16  ORD001      PART001     1         
2017-08-16  ORD002      PART002     2         
2017-08-16  ORD003      PART003     3         
2017-08-16  ORD004      PART003     4   


TableB
--------------------------------------------------------------------
Date        ORDERNUMBER PARTNUMBER  ID       PARTTYPE
---------------------------------------------------------------------
2017-08-16  ORD006      PART006     6           BAG
2017-08-16  ORD007      PART007     7           BAG
2017-08-16  ORD008      PART008     8           BAG
2017-08-16  ORD009      PART009     9           PACK
2017-08-16  ORD0010     PART0010    10          PACK

现在我想从TableB获取OrderNumber不在TableAPARTTYPE是'PACK'的值。

为此,我尝试进行内连接,但这会带来以下结果。

SELECT * 
FROM TableA A
INNER JOIN TableB B WIT(NOLOCK) ON A.ORDERNUMBER != B.ORDERNUMBER 
                                AND B.PARTTYPE = 'PACK'
WHERE A.Date = '16 AUG 2017'

联接的结果就是一切:

Date        ORDERNUMBER PARTNUMBER  ID      Date        ORDERNUMBER PARTNUMBER  ID      PARTTYPE
----------------------------------------------------------------------------------------------------------------------------------------------
2017-08-16  ORD001      PART001     1           2017-08-16O RD009       PART009     9           PACK
2017-08-16  ORD002      PART002     2           2017-08-16O RD009       PART009     9           PACK
2017-08-16  ORD003      PART003     3           2017-08-16O RD009       PART009     9           PACK
2017-08-16  ORD004      PART003     4           2017-08-16O RD009       PART009     9           PACK
2017-08-16  ORD001      PART001     1           2017-08-16O RD0010      PART0010    10          PACK
2017-08-16  ORD002      PART002     2           2017-08-16O RD0010      PART0010    10          PACK
2017-08-16  ORD003      PART003     3           2017-08-16O RD0010      PART0010    10          PACK
2017-08-16  ORD004      PART003     4           2017-08-16O RD0010      PART0010    10          PACK

我也试过一个子查询,但我知道这是错误的:

SELECT * 
FROM TableA 
WHERE ORDERNUMBER NOT IN (SELECT ORDERNUMBER 
                          FROM TableB 
                          WHERE PARTTYPE = 'PACK')

我应该一起使用NOT IN和JOINS,但是想不到它。

2 个答案:

答案 0 :(得分:2)

  

我想获取TableB中的值,其中OrderNumber不在TableA中,而PARTTYPE是'PACK'。

使用select b.* from tableb b where parttype = 'PACK' and not exists (select 1 from tablea a where a.ordernumber = b.ordernumber); 时,这基本上看起来很基本:

<Label.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding TapGestureForUpdateCategories, Source={x:Reference MainPage}}">
        <TapGestureRecognizer.CommandParameter>
            <x:Boolean>True</x:Boolean>
        </TapGestureRecognizer.CommandParameter>
    </TapGestureRecognizer>
</Label.GestureRecognizers>

答案 1 :(得分:2)

您也可以在这里进行左连接:

SELECT b.*
FROM TableB b
LEFT JOIN TableA a
    ON a.ordernumber = b.ordernumber
WHERE
    a.ordernumber IS NULL AND
    b.parttype = 'PACK'