SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,742
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help: Query to grab data from two tables

    I have one table with my users (titled 'Users') and I have another table with their comments (titled 'Comments').

    Each User has a column for RANK, and I want this current rank to be shown next to the users' comments.

    Like:

    User 1 (4)
    User 5 (2)

    And so on.

    How can I setup a mysql query that goes through the rows of comments ('Comments' table) but also grabs the user rank for each comment from the 'Users' table as well.

    'user_id' is the primary in the 'Users' table and is also a key in the 'Comments' table as well.

    Thanks
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT
      Users.username,
      Users.rank,
      Comments.title, -- is there a title on the comments?
      Comments.text
    FROM
      Comments
      JOIN Users ON (Comments.user_id = Users.user_id)
    WHERE
      Comments.topic = ?
    (Assuming that the comments are categorized according to some topic scheme. Otherwise, just delete or change the WHERE ... clause.)

    =Austin
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,742
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    worked perfect,

    thanks!
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.


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
  •