title: How to select posts created by me or my friends in a news feed?
body: <p>I've been trying to do this for quite a while now and then I figured that it is the SQL query that I cannot get it working properly in the first place.</p>
  
  <p>I am trying to create a simple news feed page similar to Facebook where you can see the post you made on your profile as well as your friends profile and also you can see posts which your friends made on your profile as well as their own profile.</p>
  
  <pre><code>SELECT 
      friendsTbl.profile_one,
      friendsTbl.profile_two,
      user_profile.u_id as my_uid,
      user_profile.f_name as my_fname,
      user_profile.l_name as my_lname,
      friend_profile.u_id as friend_uid,
      friend_profile.f_name as friend_fname,
      friend_profile.l_name as friend_lname,
      profilePostTbl.post as post,
      profilePostTbl.post_to as posted_profile,
      profilePostTbl.post_by as posted_by
  FROM friendsTbl
  LEFT JOIN profileTbl AS user_profile ON user_profile.profile_name = IF(friendsTbl.profile_one = 'john123', friendsTbl.profile_one, friendsTbl.profile_two)
  LEFT JOIN profileTbl AS friend_profile ON friend_profile.profile_name = IF(friendsTbl.profile_one = 'john123', friendsTbl.profile_two, friendsTbl.profile_one)
  LEFT JOIN profilePostTbl ON (post_by = IF(profilePostTbl.post_to = friendsTbl.profile_one,profilePostTbl.post_to, profilePostTbl.post_by))
  WHERE friendsTbl.profile_one = 'john123' OR friendsTbl.profile_two = 'john123'
  </code></pre>
  
  <p>Here's a fiddle: <a href="http://sqlfiddle.com/#!2/a10f39/1" rel="nofollow noreferrer">http://sqlfiddle.com/#!2/a10f39/1</a></p>
  
  <p>For this example, john123 is the user that is currently logged in and is friends with hassey, smith and joee and therefore only those posts must show in the news feed which john123 posted on his own or his friend's post and the ones that his friends posted on their own profile as well as on john123's profile.</p>
  
  <p>This question is a follow-up to <a href="https://stackoverflow.com/questions/25788864/php-sub-query-to-select-all-users-profile-who-are-friends-with-me-in-friends-ta">PHP Sub-query to select all user&#39;s profile who are friends with me in Friends table</a>.</p>