我正在做两个表的左外部联接,其中一个表的manager_id
可以联接到另一个表的employee_id
中的值。我编写的查询会运行,但是我试图使用nvl()
将空值替换为字符串'NO_MANAGER'。但是,这是我得到的:
select d.DEPARTMENT_ID, d.DEPARTMENT_NAME, nvl(d.manager_id, 0) AS MANAGER_ID, d.LOCATION_ID,
nvl(e.first_name || ' ' || e.LAST_NAME,'NO_MANAGER') AS NAME
from departments d
left join employees e on d.manager_id = e.EMPLOYEE_ID order by d.DEPARTMENT_ID;
这是返回的内容:
我是否缺少某些内容,或者在这种情况下
nvl()
不起作用? 最佳答案
这对您有用吗?
select d.DEPARTMENT_ID, d.DEPARTMENT_NAME,
ifnull(d.manager_id, 0) AS MANAGER_ID, d.LOCATION_ID,
if(e.first_name is null or e.LAST_NAME is null,'NO_MANAGER',
concat(e.first_name, ' ', e.LAST_NAME)) AS NAME
from departments d
left join employees e on d.manager_id = e.EMPLOYEE_ID
order by d.DEPARTMENT_ID;
?