which join to use?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • petertolla
    New Member
    • Feb 2007
    • 2

    #1

    which join to use?

    Hi,

    I have three tables that I would like to join: employee, manager_emp and manager. I would like to see all employees that have managers, along with the manager's name that can be derived from manager table. If an employee does not have a manager then just display NULL. However, I can not figure out how to join third table (manager) and still get the NULLS.



    SELECT emp.id, emp.name, emp.dept, mgre.mgrid
    FROM employee AS emp
    LEFT JOIN manager_emp AS mgre on emp.id = mgre.id

    +-----+---------+--------+--------+
    | id | name | dept | mgrid |
    |-----+---------+--------+--------+
    | 3 | fred | IT | m003 |
    | 6 | fredola | RISK | m002 |
    | 7 | reddy | NULL | NULL |


    This is the output I would like:

    +-----+---------+--------+--------+---------+
    | id | name | dept | mgrid | mgrname |
    |-----+---------+--------+--------+---------+
    | 3 | fred | IT | m003 | j james |
    | 6 | fredola | RISK | m002 | b mike |
    | 7 | reddy | NULL | NULL | NULL |

    Any assistance would be appreciated.
  • petertolla
    New Member
    • Feb 2007
    • 2

    #2
    One possible solution to this is to relax the constraint on the manager table and insert a column where the mgrid is null. I don't if this is a correct solution.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      At first sight this is a JOIN solution. But, in order to handle this properly, we have to know the structure of the 3 tables involved. How else could we find a relation? So show us the columns of the 3 tables involved.

      Ronald :cool:

      Comment

      • franklynotimpressed
        New Member
        • Feb 2007
        • 1

        #4
        i've had to created another account as the password reset facility is not working. here's the table structure

        employee

        id int(4),
        name varchar (40),
        dept varchar (30),
        bldgcode varchar(5),
        email varcahr(128)

        manager_emp

        id int(4)
        mgrid varchar(10)


        manager

        mgrid varchar(10),
        name varchar(40),
        bldgcode varchar(5),
        email varcahr(128),
        costbucker varchar(10)


        cheers

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Have a go at this one:
          Code:
          mysql> select * from employee;
          +------+---------+------+----------+-------+
          | id   | name    | dept | bldgcode | email |
          +------+---------+------+----------+-------+
          |    3 | fred    | IT   |          |       |
          |    6 | fredola | RISK |          |       |
          |    7 | reddy   | ABC  |          |       |
          +------+---------+------+----------+-------+
          mysql> select * from manager_emp;
          +------+-------+
          | id   | mgrid |
          +------+-------+
          |    3 | M01   |
          |    7 | M03   |
          +------+-------+
          mysql> select * from manager;
          +-------+----------+----------+-------+------------+
          | mgrid | name     | bldgcode | email | costbucker |
          +-------+----------+----------+-------+------------+
          | M01   | Manager1 |          |       |            |
          | M03   | Manager3 |          |       |            |
          +-------+----------+----------+-------+------------+
          Code:
          SELECT e.id,e.name,dept,manager_emp.mgrid,manager.name FROM employee as e 
          LEFT JOIN manager_emp ON(e.id=manager_emp.id) 
          LEFT JOIN manager ON(manager_emp.mgrid=manager.mgrid);
          +------+---------+------+-------+----------+
          | id   | name    | dept | mgrid | name     |
          +------+---------+------+-------+----------+
          |    3 | fred    | IT   | M01   | Manager1 |
          |    6 | fredola | RISK | NULL  | NULL     |
          |    7 | reddy   | ABC  | M03   | Manager3 |
          +------+---------+------+-------+----------+
          Ronald :cool:

          Comment

          Working...