首页 > 解决方案 > 查询未包含在另一个查询的输出中的所有剩余结果

问题描述

我在日志表中有订单,每个 order_id 可以有多个具有不同状态的行。我想返回具有后果事件的订单列表,但状态不是完成。该表如下所示:

order_number | event | status | capture_id
-------------------------------------------
    1234     | fallout |complete | 95
    1234     | fallout |in progress| 95
    1234     | fallout |Assigned| 95
    1234     | task    |           | 
    2255     | fallout |in progress| 10
    2255     | fallout |Assigned| 10
    2255     | task |         | 

在此表中,查询将返回 order_number 2255

我根据 capture_id 查询了一个列表,因为 capture_id 对于同一个 order_number 可以有多个值。与 fallout 对应的 capture_id 将在整个订单中保持不变,但在本例中为 95 和 10。

SELECT* FROM RECORDS
WHERE event = 'fallout' and status  LIKE 'complete'
order by created desc;

^ 返回所有完整的行

  SELECT * FROM  RECORDS
  WHERE capture_id NOT IN
   (SELECT capture_id FROM records
   WHERE event = 'fallout' and status  LIKE 'complete')
   order by created desc;

^这是我尝试查询当前未返回任何内容的 capture_id 的剩余部分

标签: sqloracle

解决方案


^这是我尝试查询当前未返回任何内容的 capture_id 的剩余部分

使用正确的 null 处理修复代码

SELECT *
FROM RECORDS
WHERE capture_id NOT IN
   (SELECT capture_id FROM records
   WHERE event = 'fallout' and status  LIKE 'complete' and capture_id IS NOT NULL)
order by created desc;

更多: 来自 NOT IN 子查询的奇怪结果


推荐阅读