我正在使用SQL Server 2005,我有一个这样的脚本:
select INV_Nr, INV_Date, INV_Customer
from INVOICE A,
left outer join CANCEL_INVOICE B on B.INV_Nr = A.INV_Nr
那么如何在'where'子句/过滤器中添加CANCEL_INVOICE.INV_Nr中存在的所有INVOICE.INV_Nr都不会在查询结果中显示?
谢谢,
答案 0 :(得分:3)
单向(probably the best),NOT EXISTS
:
SELECT inv_nr,
inv_date,
inv_customer
FROM invoice i
WHERE NOT EXISTS(SELECT 1
FROM cancel_invoice c
WHERE c.inv_nr = i.inv_nr)
LEFT OUTER JOIN
方法可能有效,但效率较低,导致不正确(或至少是意外)结果,因为无法区分不存在的行和存在的行但是该列为NULL的地方。
答案 1 :(得分:0)
试试这个!!
显示表CANCEL_INVOICE中不存在的所有发票A.INV_Nr
SELECT INV_Nr, INV_Date, INV_Customer
FROM INVOICE A,
LEFT OUTER JOIN CANCEL_INVOICE B ON A.INV_Nr=B.INV_Nr
WHERE B.INV_Nr IS NULL