inghfs
asked on
Subquery on the same table
Is it possible to do a subquery on the same table?
Example of what I want; A user has a manager. Therefore, the table user contains attributes: id, name, ..., managerId. Is there a (sub)query possibility to select the name of the manager directly when selecting the user?
Example of what I want; A user has a manager. Therefore, the table user contains attributes: id, name, ..., managerId. Is there a (sub)query possibility to select the name of the manager directly when selecting the user?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
but t1 = t2 in my case?
Hi,
As you said that user has a manager... mean every user is mapped to some or other manager...
you can write join between the table...
select * from xyz inner join
abc on xyz.managerid = abc.managerid
where abc.userid = '12345'
i think this will solve the issue...
As you said that user has a manager... mean every user is mapped to some or other manager...
you can write join between the table...
select * from xyz inner join
abc on xyz.managerid = abc.managerid
where abc.userid = '12345'
i think this will solve the issue...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>but t1 = t2 in my case?
"no". the table alias "t2" given to t1 makes it virtually 2 tables.
"no". the table alias "t2" given to t1 makes it virtually 2 tables.
taking anycrofts query :
select emp.id, emp.name employee_name, mgr.name manager_name
from table emp
join table emp mgr
on emp.managerId = mgr.ID
the alias mgr refers to table emp also, but allows to join to another row.
ASKER
thanks
These answers only meritted a 'B' ?
select emp.id, emp.name, mgr.name from table emp
join table mgr
on emp. managerId = mgr.ID