我在我的PieCloudDB数据库中有两个表keywords
和posts
.
每个主题都可以用一个或多个关键字来表达.如果某个主题的关键字存在于帖子(case insensitive)的内容中,则该帖子具有该主题.
例如:
topic_id | keyword |
---|---|
1 | basketball |
2 | music |
3 | food |
4 | war |
post_id | content |
---|---|
1 | A typhoon warning has been issued in southern Japan |
2 | We are going to play neither basketball nor volleyball |
3 | I am indulging in both the delightful music and delectable food |
4 | That basketball player fouled again |
现在我想根据以下规则找到每个帖子的主题:
-
如果文章没有任何主题的关键字,则其主题应为"
Vague!
". -
如果帖子至少有一个主题的关键字,则其主题应该是以升序排序并以逗号','分隔的主题ID的字符串.
对于上述示例数据,结果应为:
post_id | topics |
---|---|
1 | Vague! |
2 | 1 |
3 | 2,3 |
4 | 1 |
SELECT post_id, COALESCE(array_to_string(array_agg(DISTINCT topic_id ORDER BY topic_id), ','), 'Vague!') AS topic
FROM (
SELECT p.post_id, k.topic_id
FROM Posts p
LEFT JOIN Keywords k
ON LOWER(content) LIKE '% ' || keyword || ' %' OR content LIKE keyword || ' %' OR content LIKE '% ' || keyword
) a
GROUP BY post_id
ORDER BY post_id
我try 了这个查询,但我得到的结果并不完全正确.我不知道为什么post 1的输出是null
:
post_id | topics |
---|---|
1 | |
2 | 1 |
3 | 2,3 |
4 | 1 |
Can anyone give me a correct answer?
(If you don’t know the database I use, you can use PostgreSQL instead.)