Using subquery for the same table in MySQL

codeinprogress

I have a table called Staff which has the following fields: idStaff, Name, Phone, Email, SupervisorId.

The SuervisorId is the idStaff of that staff member's supervisor.

I want to show the list of all staff members with their basic info (Name, Email etc) as well as the name of their supervisor.

So something like this:

select idStaff
     , Name
     , Email
     , Phone
     , (select Name from Staff where idStaff = SupervisorId) as SupervisorName 
  from Staff 
 order 
    by Name ASC

The query does not work. I tried joining the two tables but I am confused on how to get the Name from the subquery in the join.

 select idStaff
      , Name
      , Phone
      , Email 
   from Staff a 
  inner 
   join Staff b 
     on a.idStaff = b.SupervisorId 
  order 
     by Name ASC

Any help will be much appreciated.

M.Ali

Maybe something like this....

select s1.idStaff
     , s1.Name
     , s1.Email
     , s1.Phone
     , s2.Name as SupervisorName 
from Staff s1
LEFT JOIN Staff s2 ON s1.SupervisorId = s2.idStaff
 order 
    by s1.Name ASC

or you could have done something like....

select s.idStaff
     , s.Name
     , s.Email
     , s.Phone
     , (select top 1 m.Name from Staff m 
                            where  s.SupervisorId =  m.idStaff) as SupervisorName 
from Staff s
order by s.Name ASC

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL Update with same table subquery

From Dev

MySQL Update with same table subquery

From Dev

subquery mysql on Update in same table

From Dev

MySQL count subquery on same table

From Dev

MYSQL Subquery on the same table with Group By Clauses...

From Dev

Mysql subquery COUNT in same Table returns NULL

From Dev

MYSQL Subquery on the same table with Group By Clauses...

From Dev

Linq subquery same table using lambda

From Dev

Update a column using a select subquery to the same table

From Dev

MySQL update a table and select from the same table in a subquery

From Dev

SQL subquery on same table

From Dev

Subquery from the same table

From Dev

Subquery in criteria - translate mysql into yii CActiveDataProvider with same table

From Dev

MYSQL - Subquery on same table in WHERE clause of UPDATE statement

From Dev

Update table in MySQL with subquery

From Dev

MySQL single table subquery

From Dev

subquery the same table in select statement

From Dev

Optimize Query with subquery on the same table

From Dev

Update with SubQuery from same table

From Dev

MySQL alternative to using a subquery

From Dev

MySql using subquery with update

From Dev

How do I update a temporary field from a subquery on the same mysql table (avoiding MySql error #1093 )

From Dev

Perform a subquery on a spatial table in MySQL

From Dev

MySQL - Referencing an aliased table in a subquery

From Dev

MySQL subquery and temporary table is slow

From Dev

MySQL subquery and temporary table is slow

From Dev

Inner Join between table and a subquery on the same table

From Dev

Mysql: using two foreign keys to the same table

From Dev

MySQL Update using data from same table

Related Related

HotTag

Archive