Skip to content

2199. Finding the Topic of Each Post

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
SELECT
  Posts.post_id,
  IFNULL(
    GROUP_CONCAT(
      DISTINCT Keywords.topic_id
      ORDER BY Keywords.topic_id
    ),
    'Ambiguous!'
  ) AS topic
FROM Posts
LEFT JOIN Keywords
  ON (CONCAT(' ', LOWER(Posts.content), ' ') LIKE CONCAT('% ', LOWER(Keywords.word), ' %'))
GROUP BY 1;