這篇文章主要為大家介紹了關於mysql中替代null的IFNULL()與COALESCE()函數的相關資料,文中透過範例程式碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看看吧。
在MySQL中isnull()
函數不能作為替代null值!
如下:
首先有個名字為business的表:
##
SELECT ISNULL(business_name,'no business_name') AS bus_isnull FROM business WHERE id=2
Incorrect parameter count in the call to native function 'isnull'
isnull()函數在mysql就行不通了。可以用
ifnull()和
coalesce()來代替。如下:
ifnull()函數:
SELECT IFNULL(business_name,'no business_name') AS bus_ifnull FROM business WHERE id=2
##當
的值不為null時:SELECT IFNULL(business_name,'no business_name') AS bus_ifnull FROM business WHERE id=1
##使用
coalesce()
SELECT COALESCE(business_name,'no business_name') AS bus_coalesce FROM business WHERE id=2
結果如下:
#
##當查詢值不為null時:
SELECT COALESCE(business_name,'no business_name') AS bus_coalesce FROM business WHERE id=1
coalesce()也可以傳回第一個不為null的值。如下:
SELECT COALESCE(business_name,district_id,id) AS bus_coalesce FROM business WHERE id=2
isnull()在mysql中怎麼用呢?答案就是用在where後面。如下:
SELECT * FROM business WHERE ISNULL(business_name)
結果如下:
#同樣,is null
和
也是用在where後面。 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:sql;">SELECT * FROM business WHERE business_name IS NULL</pre><div class="contentsignin">登入後複製</div></div>
結果如下:
SELECT * FROM business WHERE business_name IS NOT NULL
總結
以上是mysql中替代null的IFNULL()與COALESCE()函數詳解_Mysql的詳細內容。更多資訊請關注PHP中文網其他相關文章!