1. 程式人生 > >mysql中的null與空字串的區別

mysql中的null與空字串的區別

null 表示"沒有對(列)變數輸入資料"
空字串,則是"有對(列)變數輸入資料"
區別:
    1、null的長度就是null,空字串的長度就是0
    2、一串null資料比空字串優先排序
    3、count(message)會將空字串計數進去,但是不會將null資料計入
    4、可以使用繫結變數搜尋某個空字串,但是不可以這樣搜尋null

注意:select * from table where phone=null
這個 phone=null 的條件永遠不為"真"
想查詢 null 值,必須使用 is null 測試
select * from table where phone is null;
select * from table where phone='';
使用 order by expr asc,首先顯示null值,desc 會顯示最後一個
聚合函式,count(), min(), sum(), 將會忽略 null 值,但是 count(*) 將會計行數而不是單獨的列值