if(expr1,expr2,expr3) 如果expr1返回true 則執行expr2部分語句,否則執行expr3語句
//如果條件a不爲空 則使用添加a與b關聯
select a.id,b.* from a join b on if(a.id is null,1=1,a.id=b.id);
//這裏如果a.id is null,則執行1=1條件 也就是相當於沒有查詢條件 恆爲true;否則執行a.id=b.id操作
這裏的if函數與ifnull函數很類似,但是ifnull函數支持兩個參數,即如果字段爲null,則使用後面參數。如ifnull(a.id,"") as id,這裏如果a.id爲null,則將空字符賦值爲id