sql_不使用or查询 0 和null_ISNULL(a.status,0)=0 和 case when else end

 `    三种状态: 
				0 
				1
				null ` 

**查询 0 和 null **

方法一: 不使用 or isnull()

select  a.status ,a.* from t_table a  
where  ISNULL(a.status,0)=0 and a.isDelete = '0'

结果返回 : null 0 的数据


方法二:使用 or

select  a.Status ,a.* from t_table a  

where  a.isDelete = '0' and a.status = 0  
or  a.isDelete = '0' and  a.status is null
 

结果返回 :null 0 的数据


方法三 : 使用case when 和 isnull()

select a.status 
 from t_table a
where  case when a.status is null then ISNULL(a.status,0) else a.status end = 0

结果返回 :null 0 的数据


如果查询结果无返回值(查询不到数据),可用 case when + count() 进行处理

isnull((SELECT a.status FROM t_a a WHERE a.id = tc.id AND a.flag = 1),0)

SELECT a.status FROM t_a a WHERE a.id = tc.id AND a.flag = 1
上面这条sql 可能无数据返回,导致数据库会进行类型转换,可能半天也一直在查询中

优化处理:

select case when
(select count(status) FROM t_a a WHERE a.id = tc.id AND a.flag = 1) = 1
then (SELECT a.status FROM t_a a WHERE a.id = tc.id AND a.flag = 1 )
else 0 
end
)

case when 用法
http://blog.csdn.net/jobjava/article/details/27104259

http://blog.csdn.net/love_java_cc/article/details/52234415

http://www.jb51.net/article/51544.htm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值