SQL获取另一个表上不存在的数据

时间:2018-04-17 18:29:34

标签: sql ms-access

我正在尝试获取一个查询,该查询可以获取table1中存在但不存在于table2上的数据,但与数据table2具有相同的id。

在下面的示例中,我试图获得'SecID-byBank'12456,它与table2中的另外2个项目共享相同的ISIN。

我已经尝试了以下查询,但它返回了每个不在table2上的数据,而不仅仅是共享相同ISIN的数据。

查询:

Sub test2()

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

For Each cell In Worksheets("2018").Columns("T").Cells
    Set OutMail = OutApp.CreateItem(0)
    If cell.Value Like "?*@?*.?*" Then      'try with less conditions first
        With OutMail
            .To = Cells(cell.Row, "T").Value
            .Subject = "Work Order: " & Cells(cell.Row, "G").Value & " assigned"
            .Body = "Work Order: " & Cells(cell.Row, "G").Value & _
                " has been assigned to you." & _
                vbNewLine & vbNewLine & _
                "Region: " & Cells(cell.Row, "B").Value & vbNewLine & _
                "District: " & Cells(cell.Row, "C").Value & vbNewLine & _
                "City: " & Cells(cell.Row, "D").Value & vbNewLine & _
                "Atlas: " & Cells(cell.Row, "E").Value & vbNewLine & _
                "Notification Number: " & Cells(cell.Row, "F").Value & vbNewLine
            .ReadReceiptRequested = True
            .OriginatorDeliveryReportRequested = True
            .Send
        End With
        Cells(cell.Row, "V").Value = "sent"
        Set OutMail = Nothing
    End If
    Next cell

'Set OutApp = Nothing                   'it will be Nothing after End Sub
 Application.ScreenUpdating = True

 End Sub

表1

SELECT  tb1_isin, tb1_SecID_by_Bank
 FROM table1
WHERE not EXISTS (
    SELECT top 1 null
    FROM  table2
    WHERE   table1.tb1_ISIN = table2.tb2_isin
)

表2

--------------------------------------------
 Row  | SecID_by_Bank | Desc       | ISIN
--------------------------------------------
1     | 421345        |   BlaBla   | US1354
--------------------------------------------
499   | 34345         |   2.US     | XS1545
--------------------------------------------
500   | 45676         |   2/US     | XS1545
--------------------------------------------
501   | 12456         |   2-US     | XS1545
--------------------------------------------

需要查询结果:

--------------------------------------------
 Row  | SecID_by_Bank | Desc       | ISIN
--------------------------------------------
1     | 34345         |   2.US     | XS1545
--------------------------------------------
2     | 45676         |   2/US     | XS1545

我错过了什么? 谢谢!

2 个答案:

答案 0 :(得分:1)

如果您在Access中构建查询,请尝试:

SELECT Table1.* FROM Table1 WHERE (((Table1.[ISIN]) In (SELECT ISIN FROM Table2)) AND ((Table1.SecID_by_Bank) Not In (SELECT SecID_by_BANK FROM Table2)));

或使用1个子查询:

SELECT Table1.*, Table2.ISIN FROM Table2 RIGHT JOIN Table1 ON Table2.SecID_by_Bank = Table1.SecID_by_Bank WHERE (((Table1.ISIN) In (SELECT ISIN FROM Table2)) AND ((Table2.ISIN) Is Null));

答案 1 :(得分:1)

对于SQLServer中的查询:

select distinct a.SecID_by_Bank, a.[desc], a.isin from #table1 a left join #table2 b 
on a.SecID_by_Bank=b.SecID_by_Bank and a.isin=b.isin
join #table2 c on a.isin=c.isin
where b.isin is null