Jump to content

Easy Query Return


programmerwannab

Recommended Posts

Hi Guys,

 

I'm completely new to MySQL but an assignment just popped on my desk.  Can you someone pleeeease help me out with this one.  This should be easy for the gurus I can't give the real data but here is my example:

 

 

Employees table emp (below) contains rows for regular employees as well as managers. Write the query to return the manager’s name for each employee name.

Table emp:

 

Emp_id Mgr_id Emp_name

    1     2         Alex     

    2     3         Bob     

    3   NULL       Rick     

    4     2         Tom     

    5     2         Laura   

 

 

Link to comment
https://forums.phpfreaks.com/topic/38104-easy-query-return/
Share on other sites

SELECT emp.Emp_name AS Employee, mgr.Emp_name AS Manager
FROM emp LEFT JOIN emp AS mgr
ON mgr.Emp_id=emp.Mgr_id

 

This will return the name of every employee with the name of their manager (except the manager name for Rick will obviously be a NULL value - this is why LEFT JOIN is used instead of INNER JOIN; if INNER JOIN is used, Rick would be omitted from the result set). E.g.

 

|--------------------|
| Employee | Manager |
|--------------------|
| Alex     | Bob     |
| Bob      | Rick    |
| Rick     | NULL    |
| Tom      | Bob     |
| Laura    | Bob     |
|--------------------|

Link to comment
https://forums.phpfreaks.com/topic/38104-easy-query-return/#findComment-182463
Share on other sites

Hi Guys,

 

I'm completely new to MySQL but an assignment just popped on my desk.  Can you someone pleeeease help me out with this one.  This should be easy for the gurus I can't give the real data but here is my example:

 

 

Employees table emp (below) contains rows for regular employees as well as managers. Write the query to return the manager’s name for each employee name.

Table emp:

 

Emp_id Mgr_id Emp_name

    1     2         Alex     

    2     3         Bob     

    3   NULL       Rick     

    4     2         Tom     

    5     2         Laura   

 

 

 

 

But no manager’s name field in the given table...

Link to comment
https://forums.phpfreaks.com/topic/38104-easy-query-return/#findComment-182710
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.