Been stuck with this fairly simple MySQL query for a day now! Can’t believe how quickly I’ve forgotten the basics. I have 3 tables – user, post and favourite_post. The post table has a user_id that is a FK to user table.
The favourite_post table has
user_id REFERENCES user(id)
post_id REFERENCES post(id)
timestamp
When a user favourites a post, his user_id, the post_id and timestamp are inserted into the favourite_post table.
I use the following query to retrieve the 15 more recent posts
SELECT post.id, post.text, post.timestamp, post.user_id, user.username
FROM post
INNER JOIN user
ON post.user_id = user.id
ORDER BY post.id
DESC LIMIT 15;
What I need to do along with that is check if each post has been favourited by the current user(say user_id = 1) by joining with the favourite_post table.
1 Answer