I am really new in sql query and i have 1 question. Below are the details:
I have two tables:
user_table
user_id username level
-------------------------
1 adam 0
2 david 1
3 danny 2
4 siva 0
5 muthu 0
6 chong 0
managing_level
staff manager level
---------------------
1 2 1
1 3 2
4 3 2
5 2 2
I want to get all the username and their managers
expected result:
username manager1 manager2
-------------------------------
adam david danny
siva danny
muthu david
This is the query that I used:
select u.username as username,
(select username from user where user_id=m.manager and level=1) as manager1,
(select username from user where user_id=m.manager and level=2) as manager2
from user u, managing_level manager
where u.user_id=m.staff;
result:
username manager1 manager2
-------------------------------
adam david
siva danny
muthu david
As you can see, it doesn't show the second manager for adam.