2 个应该返回相同数据但没有返回相同数据的查询

发布于 2024-11-02 19:16:38 字数 790 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2024-11-09 19:16:38

每当我读到“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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文