Oracle Return 空结果时常量

Oracle Return Constant When Empty Result

我想弄清楚如何在结果集为空时return一个常量作为结果。

示例

查询将是:

select employee_id from employee where name = 'John_Doe';

本例系统中不存在李四。而不是 returning 一个空集,我怎么能 return 一个常量,比如数字 0 呢?我见过带有 case 语句的东西,但我不确定我将如何实现它。

感谢您的帮助。

当我遇到总是 return 恰好一行的问题时,我经常使用聚合。如果没有匹配,下面将 return NULL:

select max(employee_id)
from employee
where name = 'John_Doe';

以下returns 0:

select coalesce(max(employee_id), 0)
from employee
where name = 'John_Doe';