if语句
IF 语句
IF (value1,value2,value3) value1 为布尔表达式或bu布尔值 value2 当value1结果为true时返回的结果 value3 当value1结果为false时返回的结果
例1:
SELECT IF(2=1,'对','错')AS status; 结果为:对 SELECT IF(2!=1,'对','错')AS status; 结果为:错
例1测试结果:
mysql> SELECT IF(2=1,'对','错')AS status; +--------+ | status | +--------+ | 错 | +--------+ 1 row in set
mysql> SELECT IF(2!=1,'对','错')AS status; +--------+ | status | +--------+ | 对 | +--------+ 1 row in set
mysql>
例2:
SELECT IF(true,'对','错')AS status; 结果为: 对 SELECT IF(false,'对','错')AS status; 结果为: 错
例2测试结果:
mysql> SELECT IF(true,'对','错')AS status; +--------+ | status | +--------+ | 对 | +--------+ 1 row in set
mysql> SELECT IF(false,'对','错')AS status; +--------+ | status | +--------+ | 错 | +--------+ 1 row in set
mysql>
更多建议: