MySQL查询对NULL的处理

有一个字段blist,如果查询where blist <> 'B'时为什么那些blist为NULL的记录查不出?怎么写才能查出NULL值记录?
回答:
<pre name="code" id="best-content-216762161" class="best-text mb-10" style="margin-top:0px; margin-bottom:10px; padding:0px; font-family:arial,'courier new',courier,宋体,monospace; white-space:pre-wrap; word-wrap:break-word; font-size:14px; line-height:24px; background-color:rgb(255,252,246)">Null 值不能使用普通的算术运算符来比较,对这些它什么都不返回。
只能靠你自己的逻辑流程,在查询语句中再添加where blist<>'B' or blist is null;

 
 
<span style="font-family:Arial;font-size:14px;line-height:26px">在SQL中,NULL值在于任何其他值甚至NULL值比较时总是假的(FALSE)。包含NULL的一个表达式总是产生一个NULL值,除非在包含在表达式中的运算符和函数的文档中指出。在下列例子,所有的列返回NULL:</span><br style="font-family:Arial; font-size:14px; line-height:26px" /><br style="font-family:Arial; font-size:14px; line-height:26px" /><br style="font-family:Arial; font-size:14px; line-height:26px" /><span style="font-family:Arial;font-size:14px;line-height:26px">如果你想要寻找值是NULL的列,你不能使用=NULL测试。下列语句不返回任何行,因为对任何表达式,expr = NULL是假的:</span><br style="font-family:Arial; font-size:14px; line-height:26px" /><span style="font-family:Arial;font-size:14px;line-height:26px">mysql> SELECT * FROM my_table WHERE phone = NULL;</span><br style="font-family:Arial; font-size:14px; line-height:26px" /><span style="font-family:Arial;font-size:14px;line-height:26px">要想寻找NULL值,你必须使用IS NULL测试。下例显示如何找出NULL电话号码和空的电话号码:</span><br style="font-family:Arial; font-size:14px; line-height:26px" /><br style="font-family:Arial; font-size:14px; line-height:26px" /><span style="font-family:Arial;font-size:14px;line-height:26px">mysql> SELECT * FROM my_table WHERE phone IS NULL;</span><br style="font-family:Arial; font-size:14px; line-height:26px" /><span style="font-family:Arial;font-size:14px;line-height:26px">mysql> SELECT * FROM my_table WHERE phone = "";</span>
<span style="font-family:Arial;font-size:14px;line-height:26px"><span style="font-family:Arial;font-size:14px;line-height:26px">为了有助于NULL的处理,你能使用IS NULL和IS NOT NULL运算符和IFNULL()函数。</span>
</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值