表里存在NULL值导致 not in 子查询失效
--这是我第一次给业务写的SQL语句,查询来数据为0,当时没考虑太多就发出去了。 后业务给我看了几条测试数据,我发现确实查询结果不对。
--再看语句逻辑确实没错,后经过排查是BB里有几行cust_id为NULL导致not in 查询结果失效。
--改良前的语句
select * from AA a with(nolock)
where a.status_code='04' and a.issue_date>='2012-01-01' and a.issue_date<='2012-10-30' and LEFT(a.manorg_code,4)='8640' and a.cust_id not in ( select aa.cust_id from BB aa with(nolock) where aa.sheet_type='Q' and aa.acttype_code in ('01','02','16','19') )
--改良后的语句
select * from AA a with(nolock)
where a.status_code='04'
and a.issue_date>='2012-01-01' and a.issue_date<='2012-10-30'
and LEFT(a.manorg_code,4)='8640'
and a.cust_id not in
(
select ISNULL(aa.cust_id,'') from BB aa with(nolock) where aa.sheet_type='Q' and aa.acttype_code in ('01','02','16','19')
)
| 博客地址: | http://www.cnblogs.com/sword-successful/ |
| 博客版权: | 本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。 如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。 |

浙公网安备 33010602011771号