嵌套选择选择

时间:2014-07-24 20:31:42

标签: mysql sql join null nested

这是前一个问题的分支 - 我的代码看起来像这样

select  EMPLOYEE_NAME,
        HIRE_DATE,
        IFNULL(SALARY,0),
        manager_id, 
       (select ifnull(employee_name,"unkown") from employee b where b.employee_id = a.manager_id)

from employee a
order by a.EMPLOYEE_NAME;

如果没有经理值,我希望出现“unkown”这个词。

基本上是一个包含员工ID和经理ID的大表。 (并且经理ID相当于员工ID)所以我可以获得正确连接的列表,但空白是空白的......请指教。

2 个答案:

答案 0 :(得分:0)

select  EMPLOYEE_NAME,
        HIRE_DATE,
        coalesce(SALARY,0),
        manager_id, 
        coalesce(b.employee_name,'unknown') as manager_name

from employee a
left join employee b on b.employee_id = a.manager_id
order by a.EMPLOYEE_NAME;

答案 1 :(得分:0)

使用外部联接查找经理名称。

select  EMPLOYEE_NAME,
        HIRE_DATE,
        IFNULL(SALARY,0),
        IFNULL(b.EMPLOYEE_NAME, 'unknown') as 'MANAGER_NAME' 

from employee a
left outer join employee b on a.manager_id = b.employee_id
order by a.EMPLOYEE_NAME;