MySQL 中的 IFNULL
给定一个表,在这个表中,它打印表的条目。如果表为空,则它给出 NULL。
例子:
QUESTION : Given an employee table, print name from the given table which id equals to 2.
Output : Geek2
QUESTION : Given same Employee table, print name from the given table which id equals to 5.
Output : NULL
方法:在这种情况下,我们在这里使用IFNULL。如果表为空或其他条件,则 IFNULL 打印空值。
询问:-
SELECT
IFNULL(
(SELECT NAME
from employee
where id = 2),
'NULL') as NAME;
Output:-
Geek2
询问:-
SELECT
IFNULL(
(SELECT NAME
from employee
where id = 5),
'NULL') as NAME;
Output:-
NULL