使用带有完全外连接的左连接时出现问题

时间:2018-01-31 13:54:16

标签: sql vba excel-vba ms-access excel

我有以下代码:

      Dim rsD As ADODB.Recordset
      Dim sqlD As String
      Set rsD = New ADODB.Recordset
       sqlD = "SELECT A.Role FROM A LEFT JOIN (SELECT 
            DISTINCT * FROM (SELECT B.Role From B LEFT JOIN 
            C ON(B.Role=C.Role) 
            UNION ALL SELECT C.Role From C LEFT JOIN B
            ON(C.Role=B.Role))) 
            AS B ON (A.Role=B.Role) WHERE 
            (B.Role IS NULL)"
             rsD.Open sqlD, conn, adOpenStatic
             While Not rsD.EOF
            wb.Worksheets(3).Range("A2").CopyFromRecordset rsD
            Wend

             rsD.Close

    Set wb = Nothing

我想做什么:内部选择是针对FULL OUTER JOIN,所以它从表B和C中获取值并删除重复项。 然后外部选择过滤出表A中的值,但在左边连接和NULL的帮助下,表B和C中没有这些值。

但我认为外部选择存在一些问题,因为没有适当的数据。

1 个答案:

答案 0 :(得分:0)

如果我得到它,你有3个表:A,B和C. 您希望表B的值在B和C中不存在。

这是一个消失:

SELECT  A.Role 
FROM A 
    LEFT JOIN ( SELECT B.Role 
                From B 
                UNION 
                SELECT C.Role 
                From C) D ON
        D.ROLE = A.ROLE
WHERE D.ROLE IS NULL

其他地方:

SELECT  A.Role 
FROM A 
WHERE A.ROLE NOT IN (   SELECT B.Role 
                        From B 
                        UNION 
                        SELECT C.Role 
                        From C)

或仍然:

SELECT  A.Role 
FROM A 
    LEFT JOIN B ON
        B.ROLE = A.ROLE
    LEFT JOIN c ON
        c.ROLE = A.ROLE
WHERE B.ROLE IS NULL and C.ROLE IS NULL