SQL query

Discussion in 'Programming & Software Development' started by bl4ck32, Jan 23, 2015.

  1. bl4ck32

    bl4ck32 Member

    Joined:
    Aug 3, 2005
    Messages:
    4,522
    Location:
    local
    Hi all. My sql is lacking and I can't remember how to do this, so asking for some help ;)

    I have two tables, one is users that lists the ID, then name etc. Another table lists orders, and has roles on it such as manager ID, user ID, and supervisor ID. All three of these fields reference the ID field in the other table.

    Can someone remind me how to query all the names from that one ID field. It would be a multiple reference to the same table, but I cannot remember how??

    Thanks
     
  2. s.Neo

    s.Neo Member

    Joined:
    Oct 23, 2002
    Messages:
    398
    Location:
    Darwin, NT, Australia
    Something like this should do it:

    SELECT orders.*, manager.name, user.name, supervisor.name
    FROM orders
    LEFT JOIN users AS manager ON orders.managerID = manager.ID
    LEFT JOIN users AS user ON orders.userID = user.ID
    LEFT JOIN users AS supervisor ON orders.supervisorID = supervisor.ID
     

Share This Page

Advertisement: