數據庫中,一個列如果沒有指定值,那么值就為null,數據庫中的null表示“不知道”,而不是表示沒有。因此select null+1結果是null,因為“不知道”加1的結果還是“不知道”。
select * from score where english = null ;
select * from score where english != null ;都沒有任何返回結果,因為數據庫也“不知道”。
SQL中使用is null、is not null來進行空值判斷:
select * from score where english is null ; select * from score where english is not null ;
ISNULL ( check_expression , replacement_value )
select * from TblStudent
--查詢所有年齡是null的同學信息
--null值無法使用=或<>來進行比較
--unknown
--判斷null值必須使用is null或者is not null
select * from TblStudent where tsage is null
select * from TblStudent where tsage=null
--查詢所有年齡不是null的同學
select * from TblStudent where tsage<>null
select * from TblStudent where tsage is not null
select * from TblStudent where tsage=25
select * from TblStudent where tsage<>25
--任何值與null進行計算,得到的結果還是null
select 2000+null