有一个字段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>