页次: 1
if 用来控制程序的流程。 不同流程返回不同的值。
if 的格式 if("表达式", "值1", "值2") , 如果表达式的值是真的, 返回值1 , 否则返回值2
示例
mysql> select if(1, 1, 2); +-------------+ | if(1, 1, 2) | +-------------+ | 1 | +-------------+ 1 row in set (0.00 sec) mysql> select if(true, 1, 2); +----------------+ | if(true, 1, 2) | +----------------+ | 1 | +----------------+ 1 row in set (0.00 sec) mysql> select if(false, 1, 2); +-----------------+ | if(false, 1, 2) | +-----------------+ | 2 | +-----------------+ 1 row in set (0.00 sec) mysql> select if(null, 1, 2); +----------------+ | if(null, 1, 2) | +----------------+ | 2 | +----------------+ 1 row in set (0.00 sec)
ifnull(值1,值2) , 如果值1 是0,false, null 返回第一个值,否则返回第二值。
mysql> select ifnull(1,2); +-------------+ | ifnull(1,2) | +-------------+ | 1 | +-------------+ 1 row in set (0.00 sec) mysql> select ifnull(null,2); +----------------+ | ifnull(null,2) | +----------------+ | 2 | +----------------+ 1 row in set (0.00 sec) mysql> select ifnull(0,2); +-------------+ | ifnull(0,2) | +-------------+ | 0 | +-------------+ 1 row in set (0.00 sec) mysql> select ifnull(false,2); +-----------------+ | ifnull(false,2) | +-----------------+ | 0 | +-----------------+ 1 row in set (0.00 sec)
ipbbs.net
离线
页次: 1