热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

表里存在NULL值导致notin子查询失效的问题-mysql教程

表里存在NULL值导致notin子查询失效的问题--这是我第一次给业务写的SQL语句,查询来数据为0,当时没考虑太多就发出去了。后业务给我看了几条测试数据,我发现确实查询结果不对。br--再看语句逻辑确实没错,后经过排查是BB里有几行cust_id为NULL导致notin

表里存在NULL值导致not in子查询失效的问题 --这是我第一次给业务写的SQL语句,查询来数据为0,当时没考虑太多就发出去了。 后业务给我看了几条测试数据,我发现确实查询结果不对。br--再看语句逻辑确实没错,后经过排查是BB里有几行cust_id为NULL导致not in


表里存在NULL值导致not in子查询失效的问题

--这是我第一次给业务写的SQL语句,查询来数据为0,当时没考虑太多就发出去了。 后业务给我看了几条测试数据,我发现确实查询结果不对。
--再看语句逻辑确实没错,后经过排查是BB里有几行cust_id为NULL导致not in 查询结果失效。 www.2cto.com


--改良前的语句
select * from AA a with(nolock)
where a.status_code='04'

and a.issue_date>=&#39;2012-01-01&#39; and a.issue_date<=&#39;2012-10-30&#39;

and LEFT(a.manorg_code,4)=&#39;8640&#39;

and a.cust_id not in

(

select aa.cust_id from BB aa with(nolock) where aa.sheet_type=&#39;Q&#39; and aa.acttype_code in (&#39;01&#39;,&#39;02&#39;,&#39;16&#39;,&#39;19&#39;)

)

--改良后的语句

select * from AA a with(nolock)

where a.status_code=&#39;04&#39;

and a.issue_date>=&#39;2012-01-01&#39; and a.issue_date<=&#39;2012-10-30&#39;

and LEFT(a.manorg_code,4)=&#39;8640&#39;

and a.cust_id not in

(

select ISNULL(aa.cust_id,&#39;&#39;) from BB aa with(nolock) where aa.sheet_type=&#39;Q&#39; and aa.acttype_code in (&#39;01&#39;,&#39;02&#39;,&#39;16&#39;,&#39;19&#39;)

)


推荐阅读
author-avatar
waiwaiwai
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有