mysql中的条件运算
mysql中三目运算符
java中三目运算符------ 表达式 ? a : b 还有印象吗???
话不多说,先操练起来----
在mysql中三目运算符的形式----
if(表达式,a,b)
a,b不做类型限制
mysql> select if(1=1,'yes','no');
+--------------------+
| if(1=1,'yes','no') |
+--------------------+
| yes |
+--------------------+
1 row in set (0.00 sec)
mysql> select if(0,'yes','no');
+------------------+
| if(0,'yes','no') |
+------------------+
| no |
+------------------+
1 row in set (0.00 sec)
mysql> select if(null,'yes','no');
+---------------------+
| if(null,'yes','no') |
+---------------------+
| no |
+---------------------+
1 row in set (0.00 sec)
ifnull(a,b)
如果a不为null,则返回a,否则返回b
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(null,null);
+-------------------+
| ifnull(null,null) |
+-------------------+
| NULL |
+-------------------+
1 row in set (0.00 sec)
nullif(a,b)
如果a=b则返回null,否则返回a;
mysql> select nullif(1,1),nullif(1,2),nullif('对','对'),nullif('对','错');
+-------------+-------------+-------------------+-------------------+
| nullif(1,1) | nullif(1,2) | nullif('对','对'