title: How to left join multiple one to many tables in mysql?
body: <p>i have a problem with joining three tables in mysql.</p>
  
  <p>lets say we have a table named <code>posts</code> which I keep my entries in it, i have a table named <code>likes</code> which i store user_id's and post_id's in and a third table named <code>comments</code> which i store user_id's and post_id's and comment's text in it. </p>
  
  <p>I need a query that fetches list of my entries, with number of likes and comments for each entry.</p>
  
  <p>Im using this query:</p>
  
  <pre><code>SELECT posts.id, count(comments.id) as total_comments, count(likes.id) as total_likes
  FROM `posts`
  LEFT OUTER JOIN comments ON comments.post_id = posts.id 
  LEFT OUTER JOIN likes ON likes.post_id = posts.id
  GROUP BY posts.id
  </code></pre>
  
  <p>but there is a problem with this query, if comments are empty for an item, likes count is just ok, but lets say if an entry has 2 comments and 4 likes, both total_comments and total_likes will be "8", meaning that mysql multiplies them.
  I'm confused and I dont know what whould I do.</p>
  
  <p>Thanks in advace.</p>