SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    318
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Joining the same table twice.

    Have a table in which I need to get two user's usernames from it. I.e.

    challenge(id, challengerid, receiverid)

    challengerid and receiverid are ids from the member table.

    member(id, username)

    how do I get both the challengerid's username aswell as the receiverid's username with the same query?

    Any help would be awesome!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Just like you say in the title... join the same table twice, using aliasses to give them a unique name in the query:
    Code:
    FROM challenge
    INNER JOIN members AS m1 
    ON...
    INNER JOIN members AS m2
    ON...

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,273
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    i don't like the generic aliases when there are much nicer ones available
    Code:
    SELECT challenge.id
         , challenger.username AS challenger
         , receiver.username AS receiver
      FROM challenge
    INNER
      JOIN member AS challenger
        ON challenger.id = challenge.challengerid
    INNER
      JOIN member AS receiver
        ON receiver.id = challenge.receiverid
    i like this better than m1 and m2
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    318
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Awesome thanks guys!


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
  •