使用NOT IN时,BETWEEN条件不起作用

时间:2013-11-22 06:47:09

标签: sql

SELECT *
FROM Customer 
INNER JOIN Ledger Entry on [No_] = [Customer No_]
WHERE No_ NOT IN ([Posting Date] between '2013-10-01' and '2013-10-31')

BETWEEN条件不起作用。

2 个答案:

答案 0 :(得分:4)

这不是not in的工作原理,您将No_看似客户的IdPosting Date进行比较,这样做会失败。

我想你想要这样的东西:

SELECT *
FROM Customer c
inner join [Ledger Entry] le on c.[No_] = le.[Customer No_]
where c.[No_] not in (
                        select l.[Customer No_] 
                        from [Ledger Entry] l 
                        where  l.[Posting Date] between '2013-10-01' and '2013-10-31'
                     )

但是对于这样的查询,它应该简化为:

SELECT *
FROM 
Customer c
inner join [Ledger Entry] le on c.[No_] = le.[Customer No_]
where le.[Posting Date] < '2013-10-01' or le.[Posting Date] > '2013-10-31'

根本不使用not inbetween

答案 1 :(得分:0)

首先,您的日期应与另一个日期类型列进行比较。
其次,根据Aaron Bertrand,您应该避免使用BETWEEN作为日期,您可以通过文章了解他是对的。
所以,我建议你继续;

SELECT * FROM Customer 
inner join LedgerEntry 
on Customer.[No_] = LedgerEntry.[Customer No_] 
where LedgerEntry.[Posting Date] < '2013-10-01' and LedgerEntry.[Posting Date] > '2013-10-31'