sql - 查询未包含在另一个查询的输出中的所有剩余结果
问题描述
我在日志表中有订单,每个 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 的剩余部分
解决方案
^这是我尝试查询当前未返回任何内容的 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;
推荐阅读
- mysql - 如何从 SQL 中的特定字符中拆分字符串?
- swiftui - SwiftUI List 查看不同索引的不同单元格
- bash - Shell 脚本 - 抑制和添加行
- android - 如何在我的构建中获得适用于统一视频的 agora 视频 SDK?
- reactjs - 使用 redux 钩子的酶测试错误:“找不到 react-redux 上下文值;请确保组件包装在
" - php - 表单、PHP(自制)函数和刷新页面的怪异行为
- ios - 使用 Expo 和 Firebase 的 iOS 应用奖励推荐
- reactjs - 不在材料表中验证删除
- javascript - 尝试使用 Odin 项目的基本 HTML 计算器计算连续操作数
- python - 更新 Django ImageField 中的个人资料图片:Django Admin 中使用的动态方法