title: MySQL - Searching with UNION ALL and GROUP BY
body: <pre><code>SELECT p.id, p.title, p.uri, 'post' AS search_type
  FROM `posts` AS p 
  WHERE title LIKE "%logo%" 
  
  UNION ALL 
  
  SELECT p.id, p.title, p.uri, 'tag' AS search_type
  FROM posts AS p 
  INNER JOIN post_tags AS pt ON pt.post_id = p.id 
  INNER JOIN tags AS t ON pt.tag_id = t.id 
  WHERE t.title LIKE "%logo%"
  
  UNION ALL
  
  SELECT p.id, p.title, p.uri, 'category' AS search_type
  FROM posts AS p 
  INNER JOIN post_categories AS pc ON pc.post_id = p.id 
  INNER JOIN categories AS c ON pc.category_id = c.id 
  WHERE c.title LIKE "%logo%"
  
  GROUP BY p.id
  LIMIT 30
  </code></pre>
  
  <p>I am trying to group the post ID's so I do not return duplicate search results, but for some reason there are duplicates even when I use the <code>GROUP BY p.id</code>. Can someone tell me what I am doing wrong?</p>