ive been trying to link these 5 tables together because i am creating a wall feed where posts are gotten from a user and their friend. However i am finding it difficult to select all data from these 5 tables without having mysqli having an error. This is how the database looks like.
I have a POSTS TABLES I Also have a USERS TABLE
Along with the FRIENDS TABLE
Now depending on the post type: i have a photos table, as such
The problem is how do i link all these tables together and output them as rows for a wall feed. Please i've been racking my brain
SELECT p.id, p.post_title, p.post_category, p.post_content, u.username
FROM post_table as p, user_table as u, friends_table as f
WHERE u.user_id = p.user_id and (f.user1 = p.user_id or f.user2 = p.user_id)
This is just a simple way how to get data from related table.
You could combine the current user's posts with their friends' posts into a single stream with several left outer joins
:
select
posts.*,
users.*,
photos.photo_source
from posts
join users
on (users.user_id = posts.user_id)
left join friends
on (friends.user1 = ? and friends.user2 = posts.user_id)
left join photos
on (photos.post_id = posts.id)
where
posts.user_id = ? or not isnull(friends.user2)
(Change ? to the user's ID.)
You'd be retrieving the posts where the user_id
matches the current user or one of his friends (friends.user2
). The photos
table would also be selected with a left outer join
.