0

我正在对两个表进行左外连接,其中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()

4

3 回答 3

0

这对你有用吗:

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;

?

于 2015-12-04T01:39:13.237 回答
0

发生这种情况是因为当您放置:

e.first_name || ' ' || e.LAST_NAME

自动将您与字符串连接的空值转换为与空值不同的字符串值,这是一个隐式转换,并且 nvl 找不到空值。

您可以试试这个(当然也可以尝试其他解决方案)

select d.DEPARTMENT_ID, d.DEPARTMENT_NAME, nvl(d.manager_id, 0) AS MANAGER_ID, d.LOCATION_ID, 
nvl(e.first_name,'NO_MANAGER') || nvl(e.LAST_NAME,'') AS NAME 
from departments d
left join employees e on d.manager_id = e.EMPLOYEE_ID order by d.DEPARTMENT_ID;
于 2015-12-07T01:18:20.627 回答
0

找到了一个显示我正在寻找的结果的解决方案:

TRIM(LEADING FROM e.FIRST_NAME || ' ' || COALESCE(e.LAST_NAME, 'No Manager')) AS "NAME"
于 2015-12-07T00:02:40.633 回答