Search code examples
sqlmysqlchaining

Combine SQL statement


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 

Solution

  • 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