title: Joining multiple tables based on value
body: <p>I have a PHP/MYSQL website through which I post two types of content:</p>
  
  <ol>
  <li>News Posts (my content)</li>
  <li>Visitor Submissions (poetry, artwork etc)</li>
  </ol>
  
  <p>Each of these content types has its own individual table, and each has a column that stores the title of the post/submission. There is a third table that I use to store the comments for both the news posts and submissions. I set up the comments table to have a column called <strong>content_category</strong> which would add the value of <em>"post"</em> or <em>"submission"</em> to track which of the content types it belongs to. There is also another column that stores the primary key/id value of the news post / visitor submission called <strong>content_id</strong>.</p>
  
  <p>The idea is that when pulling the comments from the database, I want to also display the title of the post or the submission. The logic I want to implement is basically this: If the record has a content_category column value of "post", JOIN the POSTS table on post_ID, but IF the content_category column has a value of "submission", JOIN the SUBMISSIONS table on submission_id.</p>
  
  <p>This is my code at the moment but it's not working :(</p>
  
  <pre><code>SELECT * FROM comments 
  LEFT JOIN posts AS details ON comments.content_id = details.post_id
  WHERE comments.content_category = 'post'
  
  UNION
  
  SELECT * FROM comments 
  LEFT JOIN submissions AS details ON comments.content_id = details.submission_id
  WHERE comments.content_category = 'submission'
  </code></pre>
  
  <p>Your kind help is much appreciated :(</p>