2 queries that should return the same data but don't
I have 2 Informix queries that I believe should return the same data but do not. The first query uses a subquery as a filter and improperly returns no rows. The second is performed using a left outer join checking for null on the same column used in the subquery and it properly returns the correct data set. Am I missing something or is this a bug?
select i.invoice_date, oe.commit_no
from oe
join 开发者_运维技巧invoice i
on oe.invoice_no = i.invoice_no
where i.invoice_date > today - 60
and oe.commit_no not in (select commit_no from bolx)
select i.invoice_date, oe.commit_no, bolx.bol_no
from oe
join invoice i
on oe.invoice_no = i.invoice_no
left join bolx
on bolx.commit_no = oe.commit_no
where i.invoice_date > today - 60
and bolx.commit_no is null
Abbreviated schemas (this is a legacy db, so it's got some quirks):
invoice
invoice_no char(9),
invoice_date date
oe
commit_no decimal(8, 0),
invoice_no char(9)
bolx
commit_no decimal(8, 0)
Any time I read "Not In ... subquery ... returns no rows" I'm pretty sure I know the answer!
I presume select commit_no from bolx
returns some NULL
values?
The presence of a NULL
in a NOT IN
guarantees that no results will be returned.
foo NOT IN (bar, NULL)
is equivalent to
foo <> bar and foo <> NULL
The foo <> NULL
part will always evaluate to unknown
and the AND
can never evaluate to true
unless all conditions evaluate to true
.
精彩评论