title: PHP & Mysql - Left Outer Join between two tables
body: <p>I have two tables called 'events' and 'topics' each table can have many comments.</p>
  
  <p>What I need to do is list all the events and topics with the amount of comments for each row. I've managed to return all the topics, which works great but I don't know how I can add the events table to the MySql. The comments and events table fields are listed below. Can anyone help me with this query?</p>
  
  <p>Events:</p>
  
  <ul>
  <li>ID</li>
  <li>Event_Name</li>
  </ul>
  
  <p>Comments:</p>
  
  <ul>
  <li>post_id &lt;-- the releated id for either the events or topics table</li>
  <li><p>table &lt;-- The table that the row belongs to so either topics or events</p>
  
  <pre><code>SELECT 
    t.id, t.title, c.created_at, 
    IF(ISNULL(c.allComments), 0, c.allComments) AS totalComments
  FROM topics AS t
  LEFT OUTER JOIN (
      SELECT created_at, post_id, COUNT(*) AS allComments 
      FROM comments
      GROUP BY post_id
  ) AS c ON c.post_id = t.id
  ORDER BY tc.created_at DESC, c.allComments DESC
  </code></pre></li>
  </ul>