SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    Italy
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with multiple joins

    I have two tables, equipment and users. In the equipment table I have two field, owner and technician which both link to the users table id.

    I can get the owner's name using a single join, but not sure how to get the technician's name

    Code:
    SELECT *, name, email FROM equipment 
    LEFT JOIN users ON equipment.owner = users.id
    I've tried to use aliases but have had no success.

    thanks

  2. #2
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Need to see the tables' detaisl

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,250
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT equipment.*
         , owner.name AS owner_name
         , owner.email AS owner_email
         , technician.name AS technician_name
         , technician.email AS technician_email
      FROM equipment 
    INNER
      JOIN users AS owner
        ON owner.id = equipment.owner 
    INNER
      JOIN users AS technician
        ON technician.id = equipment.technician
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2003
    Location
    Italy
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks r937.

    perfect


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •