开发者

Combine SQL statement

开发者 https://www.devze.com 2022-12-31 06:03 出处:网络
I have 3 tables (follows, postings, users) follows has 2 fields -> profile_id , following_id postings has 3 fields -> post_id, profile_id, content

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.

开发者_如何学C

Thank you for your help! :)

Ex:

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


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


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


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 


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
0

精彩评论

暂无评论...
验证码 换一张
取 消