0

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
Brian Byrne
  • 107
  • 1
  • 5
  • 17

1 Answers1

1
SELECT <columns>
FROM (
    SELECT <columns>
    FROM <friends criteria>
    UNION
    SELECT <columns>
    FROM <current user criteria>
) x
order by msg_id desc
limit 10
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • It's not that simple though. There are joins involved, together with an order by which I need. – Brian Byrne Mar 04 '12 at 11:36
  • I don't understand - there's no where clause. – Brian Byrne Mar 04 '12 at 11:53
  • I haven't spoon-feed you the answer... this is "meta SQL": I've left the unimportant stuff out (selected columns, the `join` crap and `where` crap) to show you just the *core* of a solution. You should be able to "fill in the blanks". BTW, if you get the inner query right, this will order the rows correctly. – Bohemian Mar 05 '12 at 07:51