1,if (value, t, f)
mysql> select if (salary > 2000, 'high', 'low') from salary;
+-----------------------------------+
| if (salary > 2000, 'high', 'low') |
+-----------------------------------+
| low |
| low |
| high |
| high |
| high |
| low |
+-----------------------------------+
6 rows in set (0.01 sec)
如果salary大于2000 ,显示high,否则显示low
2, ifnull(value1, value2);
mysql> select ifnull(salary, 0) from salary;
+-------------------+
| ifnull(salary, 0) |
+-------------------+
| 1000.00 |
| 2000.00 |
| 3000.00 |
| 4000.00 |
| 5000.00 |
| 0.00 |
+-------------------+
6 rows in set (0.00 sec)
如果value1为null,那么用value2替换value1的值
3,case when values then values2 else values3 end
mysql> select case when salary <= 2000 then 'low' else 'high' end from salary;
+-----------------------------------------------------+
| case when salary <= 2000 then 'low' else 'high' end |
+-----------------------------------------------------+
| low |
| low |
| high |
| high |
| high |
| high |
+-----------------------------------------------------+
6 rows in set (0.00 sec)
salary小于等于2000时,显示low,否则显示high