剧情还原:

上周投产之后同事应该在某个页面查询的时候新增了一个not in的条件,结果导致列表部分数据不展示了,经排查发现原来是not in条件的问题。

问题复现:

1.基础数据: 共10条数据 状态为空-1条 已作废-4条 报告审批-5条

select * from v_safe_testengineer v where v.vst_tester ='w07387';

2.新增not in ‘已作废’条件之后:共5条数据 报告审批-5条 什么情况?状态为空的数据怎么不见了?

select * from v_safe_testengineer v where v.vst_tester ='w07387' and v.vst_note not in '已作废';

预期结果:共6条数据 状态为空-1条 报告审批-5条

解决方案:

3.改进SQL之后:与预期结果一致

select * from v_safe_testengineer v where v.vst_tester ='w07387' and (v.vst_note not in '已作废' or v.vst_note is null );

ps:这种方案也能解决该问题,但是大家都知道Oracle in 超过1000个参数会报 “ORA-01795: 列表中的最大表达式数为 1000”

4.最终方案>达到预期结果

select * from v_safe_testengineer v where v.vst_tester ='w07387' 
and not exists (select a.id from v_safe_testengineer a where a.vst_tester ='w07387' and a.vst_note = '已作废' and a.id = v.id)

结论:

大家拼接SQL条件的时候如果字段值有NULL的情况不要使用not in 这种方式,还是建议大家使用 not exists 和 exists ,相对来说效率高一点。
最后修改:2022 年 04 月 04 日
如果觉得我的文章对你有用,请随意赞赏