caner taşdemir caner taşdemir - 10 months ago 59
MySQL Question

Select posts from users which are followed

I have spent hours and still couldn't able to do it correctly. I have user table, and posts table.

User table

username | followers | following

john | mary,steven,joel | anthony,matthew

Posts table

fromuser | post | date

mary | Bla bla, today is cold. | 1475982647

I need to get posts from the users who are being followed. Tried this;

SELECT * FROM posts WHERE $member[nick]
IN (SELECT followers FROM users WHERE $member[nick] IN followers ORDER BY id DESC limit 10

This doesn't return any posts. What is the correct way to do it ?

Answer Source
FROM posts
                  (SELECT following FROM users WHERE username = 'john')) > 0

This query uses FIND_IN_SET to check each user appearing in posts whether or not he is being followed by a given user (John in this case).