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
本文介绍了MySQL中三种常用的条件判断函数:if(), ifnull(), case when...else...end 的使用方法及示例。通过具体示例展示了如何根据薪资字段判断高低级别、如何处理NULL值以及如何实现更复杂的条件判断。
5444

被折叠的 条评论
为什么被折叠?



