您的位置:首页 > 数据库 > MySQL

MySQL查询对NULL的处理

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


<span style="font-family: Arial; line-height: 26px;">在SQL中,NULL值在于任何其他值甚至NULL值比较时总是假的(FALSE)。包含NULL的一个表达式总是产生一个NULL值,除非在包含在表达式中的运算符和函数的文档中指出。在下列例子,所有的列返回NULL:</span><br style="font-family: Arial; line-height: 26px;" /><br style="font-family: Arial; line-height: 26px;" /><br style="font-family: Arial; line-height: 26px;" /><span style="font-family: Arial; line-height: 26px;">如果你想要寻找值是NULL的列,你不能使用=NULL测试。下列语句不返回任何行,因为对任何表达式,expr = NULL是假的:</span><br style="font-family: Arial; line-height: 26px;" /><span style="font-family: Arial; line-height: 26px;">mysql> SELECT * FROM my_table WHERE phone = NULL;</span><br style="font-family: Arial; line-height: 26px;" /><span style="font-family: Arial; line-height: 26px;">要想寻找NULL值,你必须使用IS NULL测试。下例显示如何找出NULL电话号码和空的电话号码:</span><br style="font-family: Arial; line-height: 26px;" /><br style="font-family: Arial; line-height: 26px;" /><span style="font-family: Arial; line-height: 26px;">mysql> SELECT * FROM my_table WHERE phone IS NULL;</span><br style="font-family: Arial; line-height: 26px;" /><span style="font-family: Arial; line-height: 26px;">mysql> SELECT * FROM my_table WHERE phone = "";</span>
<span style="font-family: Arial; line-height: 26px;">为了有助于NULL的处理,你能使用IS NULL和IS NOT NULL运算符和IFNULL()函数。</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: