programmerwannab Posted February 12, 2007 Share Posted February 12, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/38104-easy-query-return/ Share on other sites More sharing options...
iamgregg Posted February 12, 2007 Share Posted February 12, 2007 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 | |--------------------| Quote Link to comment https://forums.phpfreaks.com/topic/38104-easy-query-return/#findComment-182463 Share on other sites More sharing options...
worldworld Posted February 12, 2007 Share Posted February 12, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/38104-easy-query-return/#findComment-182710 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.