2 个应该返回相同数据但没有返回相同数据的查询
我有 2 个 Informix 查询,我认为它们应该返回相同的数据,但没有。第一个查询使用子查询作为过滤器,并且不正确地不返回任何行。第二个是使用左外连接执行的,检查子查询中使用的同一列是否为 null,并且它正确返回正确的数据集。我错过了什么还是这是一个错误?
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
缩写模式(这是一个遗留数据库,所以它有一些怪癖):
invoice
invoice_no char(9),
invoice_date date
oe
commit_no decimal(8, 0),
invoice_no char(9)
bolx
commit_no decimal(8, 0)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每当我读到“Not In ... subquery ... returns no rows”时,我很确定我知道答案!
我假设
select commit_no from bolx
返回一些NULL
值?NOT IN
中存在NULL
可保证不会返回任何结果。foo NOT IN (bar, NULL)
相当于foo <>; bar 和 foo <> NULL
foo <>; NULL
部分将始终评估为unknown
,并且AND
永远不会评估为true
,除非所有条件评估为true< /代码>。
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 someNULL
values?The presence of a
NULL
in aNOT IN
guarantees that no results will be returned.foo NOT IN (bar, NULL)
is equivalent tofoo <> bar and foo <> NULL
The
foo <> NULL
part will always evaluate tounknown
and theAND
can never evaluate totrue
unless all conditions evaluate totrue
.