如何通过使用MySQL IF()函数自定义行的值而不是NULL?
假设在“雇员”表中,我们有NULL作为两名雇员的“工资”列的值。如下所示的数据本身没有意义。
Select * from employee; +----+--------+--------+ | ID | Name | Salary | +----+--------+--------+ | 1 | Gaurav | 50000 | | 2 | Rahul | 20000 | | 3 | Advik | 25000 | | 4 | Aarav | 65000 | | 5 | Ram | 20000 | | 6 | Mohan | 30000 | | 7 | Aryan | NULL | | 8 | Vinay | NULL | +----+--------+--------+ 8 rows in set (0.22 sec)
通过使用IF()函数返回'N/A'而不是NULL,我们可以避免在输出中显示NULL。
Select ID,Name,IF(Salary IS NULL, 'N/A',Salary) As 'Salary' from employee; +----+--------+--------+ | ID | Name | Salary | +----+--------+--------+ | 1 | Gaurav | 50000 | | 2 | Rahul | 20000 | | 3 | Advik | 25000 | | 4 | Aarav | 65000 | | 5 | Ram | 20000 | | 6 | Mohan | 30000 | | 7 | Aryan | N/A | | 8 | Vinay | N/A | +----+--------+--------+ 8 rows in set (0.00 sec)