1. 程式人生 > >mySql中IFNULL的使用說明

mySql中IFNULL的使用說明

 

IFNULL(expr1,expr2)

如果expr1不是NULL,IFNULL()返回expr1,否則它返回expr2。IFNULL()返回一個數字或字串值

具體用法如:現有學生表(tbl_student)和分數表(score),查詢學生表的所有欄位和學生相對於的英語成績(english_score)sql如下:

select stu.*,IFNULL(score.english_score,0) from tbl_student stu,tbl_score score where 1=1 and stu.stu_id=score.stu_id

以上sql中,如果score表中的english_score有值,則IFNULL(score.english_score,0)就顯示english_score,否則,顯示0