子查询在“NOT IN”中返回NULL

时间:2013-10-22 08:10:59

标签: sql vb.net ms-access

这是我的查询

select distinct(Challan_No) 
from  Challan_tb 
where Challan_No not in 
  (
    select Challan_No from Invoice_tb where Customer_ID =2 and InvYear=2013
  ) and InvYear=2013 and Customer_ID =2

子查询返回NULL,因此外部查询无法正常工作

请帮忙

2 个答案:

答案 0 :(得分:2)

如果您不想过多地更改查询结构,那么这是一个简单而明显的修复:

select distinct(Challan_No) 
from  Challan_tb 
where Challan_No not in 
  (
    select Challan_No from Invoice_tb where Customer_ID =2 and InvYear=2013
    and Challan_No is not null --This line is new
  ) and InvYear=2013 and Customer_ID =2

答案 1 :(得分:0)

您可以尝试LEFT JOIN。我认为Access会更好地消化它。

select distinct(Challan_tb.Challan_No) 
from Challan_tb 
left join Invoice_tb on Invoice_tb.Challan_No = Challan_tb.Challan_No
where Challan_tb.InvYear=2013 and Challan_tb.Customer_ID=2
and Invoice_tb.InvYear is null --or any other Invoice_tb field