views:

61

answers:

4

I have 3 tables (follows, postings, users)

follows has 2 fields -> profile_id , following_id

postings has 3 fields -> post_id, profile_id, content

users has 3 fields -> profile_id, first_name, last_name

I have a follows.profile_id value of 1 that I want to match against.

When I run the SQL statement below I get the 1st step in obtaining the correct data. However, I now want to match the postings.profile_id of this resulting set against the users table so each of the names (first and last name) are displayed as well for all the listed postings.

Thank you for your help! :)

Ex:

  SELECT * 
    FROM follows 
    JOIN postings ON follows.following_id = postings.profile_id 
   WHERE follows.profile_id = 1 
+1  A: 

Use:

  SELECT p.*,
         u.first_name,
         u.last_name
    FROM POSTINGS p
    JOIN FOLLOWS f ON f.following_id = p.profile_id
                  AND f.profile_id = 1
    JOIN USERS u ON u.profile_id = p.profile_id
ORDER BY tweets.modified DESC
OMG Ponies
+1  A: 
SELECT * 
FROM follows, postings, users
WHERE follows.following_id = postings.profile_id 
AND users.profile_id = follows.profile_id
AND follows.profile_id = 1 
ORDER BY tweets.modified DESC 
Randy
+1  A: 

Seems easy enough to extend the join to another table... maybe I am missing some part of the question!

    select 
      f.*,
      p.*,
      u.*
    from 

      follows f

          inner join 
      postings p
          on f.following_id = p.profile_id

          inner join
      users u
          on p.profile_id = u.profile_id


    where follows.profile_id = 1
Joe Koberg
+1  A: 

use:

SELECT * 
    FROM follows 
    JOIN postings ON follows.following_id = postings.profile_id 
    INNER JOIN users on postings.profile_id = users.profile_id
   WHERE follows.profile_id = 1 
ORDER BY tweets.modified DESC
jpabluz
Thank you very much, easy and straight forward to use! :)
ninumedia