2 queries that should return the same data but don't

这一生的挚爱 提交于 2020-01-16 06:27:11

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/5750701/2-queries-that-should-return-the-same-data-but-dont

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!