Get updates from current user and all their friends - mysql query
-
05-12-2019 - |
Question
I'm building a Facebook-like wall and wish to retrieve all updates from both the current user and all their friends.
As it stands I'm only getting updates from user's friends. How do I include user's own updates too?
SELECT M.msg_id,
M.uid_fk,
M.message,
M.created,
U.fname,
U.lname,
M.uploads
FROM messages M INNER JOIN users_friends F ON F.friendID = M.uid_fk
AND F.userID = " . $_SESSION['user_id'] . "
AND F.status = 2
INNER JOIN users U ON U.userID = F.friendID
order by M.msg_id desc limit 10
Solution
SELECT <columns>
FROM (
SELECT <columns>
FROM <friends criteria>
UNION
SELECT <columns>
FROM <current user criteria>
) x
order by msg_id desc
limit 10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow