IF 語(yǔ)句
IF (value1,value2,value3) value1 為布爾表達(dá)式或bu布爾值 value2 當(dāng)value1結(jié)果為true時(shí)返回的結(jié)果 value3 當(dāng)value1結(jié)果為false時(shí)返回的結(jié)果
例1:
SELECT IF(2=1,'對(duì)','錯(cuò)')AS status; 結(jié)果為:對(duì) SELECT IF(2!=1,'對(duì)','錯(cuò)')AS status; 結(jié)果為:錯(cuò)
例1測(cè)試結(jié)果:
mysql> SELECT IF(2=1,'對(duì)','錯(cuò)')AS status; +--------+ | status | +--------+ | 錯(cuò) | +--------+ 1 row in set
mysql> SELECT IF(2!=1,'對(duì)','錯(cuò)')AS status; +--------+ | status | +--------+ | 對(duì) | +--------+ 1 row in set
mysql>
例2:
SELECT IF(true,'對(duì)','錯(cuò)')AS status; 結(jié)果為: 對(duì) SELECT IF(false,'對(duì)','錯(cuò)')AS status; 結(jié)果為: 錯(cuò)
例2測(cè)試結(jié)果:
mysql> SELECT IF(true,'對(duì)','錯(cuò)')AS status; +--------+ | status | +--------+ | 對(duì) | +--------+ 1 row in set
mysql> SELECT IF(false,'對(duì)','錯(cuò)')AS status; +--------+ | status | +--------+ | 錯(cuò) | +--------+ 1 row in set
mysql>
更多建議: