Question

I have the table structure like below,

employees_info :

id | name | role   | managerid | 
1  | john | BO     | 2         |
2  | jack | Manager| 3         |

Now I need to select the john's details where I need his id, name, role and managername (not manager id). As per the table above I need to write a query for the same table as manager is also there in the same table.

I thought of using union like below,

select * from employees_info e
union (select emp_name as manager_name
from employees_info 
where e.managerid = managerid);

but I got

unknown column e.managerid in where clause

I can create role as a different table and I can use inner join to get it working, but, I just thought of trying it once like the above approach and I am unable to get it working. Please help me solve this issue if its a right way. Or you can even give me a better approach for this.

Was it helpful?

Solution

SQLFiddle example here

select E.id emp_id, E.name emap_name, E.role emp_role, M.name manager_name from employees_info E inner join employees_info M on E.managerid = M.id where E.name='john'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top