title: full joining 3 tables and ordering by one
body: <h2><strong>This is giving me a headache. I'll try to give a good example based on a database of user posts.</strong></h2>
  
  <p><strong>Table1</strong> has the BODY of the post along with a unique POSTID and USERNAME of the author.</p>
  
  <p><strong>Table2</strong> carries the number of "likes" a post has (LIKES) referenced by POSTID (which matches the postid from Table1).</p>
  
  <p>Users are allowed to "share" other people's posts on their page. So...</p>
  
  <p><strong>Table3</strong> carries the POSTID of the shared post along with the USERNAME of the user sharing it.</p>
  
  <p>On the user's page you want to display a listing of his posts along with the posts he wants to share. So we need to grab all of the POSTIDs of the posts from table1 where he is the USERNAME and add to that all the POSTIDs from table3 where he is the USERNAME, and then order all of the POSTIDs by the number of LIKES in table2.</p>
  
  <p>This is what I tried:</p>
  
  <pre><code>SELECT postid 
  FROM posts_views_likes 
  LEFT JOIN posts USING (postid) 
  LEFT JOIN shared_posts USING (postid) 
  WHERE posts.username = 'abbsmalone' 
  UNION 
  SELECT postid 
  FROM posts_views_likes 
  LEFT JOIN posts USING (postid) 
  LEFT JOIN shared_posts USING (postid) 
  WHERE shared_posts.username = 'abbsmalone' 
  ORDER BY posts_views_likes.likes DESC 
  </code></pre>
  
  <p>I also tried...</p>
  
  <pre><code>SELECT postid 
  FROM posts 
  LEFT JOIN posts_views_likes USING (postid) 
  LEFT JOIN shared_posts USING (postid) 
  WHERE posts.username = 'abbsmalone' 
  UNION 
  SELECT postid 
  FROM shared_posts 
  LEFT JOIN posts_views_likes USING (postid) 
  LEFT JOIN posts USING (postid) 
  WHERE shared_posts.username = 'abbsmalone' 
  ORDER BY posts_views_likes.likes DESC 
  </code></pre>
  
  <p>Obviously, that doesn't work. I keep getting an error saying  #1054 - Unknown column 'likes' in 'order clause'</p>
  
  <p>I assume such a thing is possible?</p>
  
  <p>UPDATE:</p>
  
  <p>I also tried:</p>
  
  <pre><code>SELECT postid, posts_views_likes.likes 
  FROM posts 
  LEFT JOIN posts_views_likes USING (postid) 
  LEFT JOIN shared_posts USING (postid) 
  WHERE posts.username = 'abbsmalone' 
  UNION 
  SELECT postid, posts_views_likes.likes 
  FROM posts_views_likes 
  LEFT JOIN posts USING (postid) 
  LEFT JOIN shared_posts USING (postid) 
  WHERE shared_posts.username = 'abbsmalone' 
  UNION 
  SELECT postid, posts_views_likes.likes 
  FROM shared_posts 
  LEFT JOIN posts_views_likes USING (postid) 
  LEFT JOIN posts USING (postid) 
  WHERE posts.username = 'abbsmalone' OR shared_posts.username = 'abbsmalone' 
  ORDER BY posts_views_likes.likes DESC 
  </code></pre>
  
  <p>But I get the same error.</p>
  
  <p>UPDATE FIXED:</p>
  
  <p>I finally figured it out and it was a lot easier than I was trying to make it. </p>
  
  <pre><code>SELECT postid, posts_views_likes.ratio, posts.username 
  FROM posts 
  LEFT JOIN posts_views_likes USING (postid) 
  LEFT JOIN shared_posts USING (postid) 
  WHERE posts.username = 'abbsmalone' 
  OR shared_posts.username = 'abbsmalone' 
  ORDER BY posts_views_likes.ratio DESC
  </code></pre>