【LeetCode2199. 找到每篇文章的主题】字符串处理题,使用MySQL里的group_concat和LOCATE函数完成

yhm138 / 2023-08-19 / 原文

题目地址

https://leetcode.cn/problems/finding-the-topic-of-each-post/description/

代码

with t1 as(
    select p.*,  k.*
    from Posts p 
    left join Keywords k
    on LOCATE(LOWER(CONCAT(' ', word, ' ')), LOWER(CONCAT(' ', content, ' '))) >0
)

select post_id, 
ifnull(group_concat(distinct topic_id order by topic_id asc separator "," ),"Ambiguous!") as  topic
from t1
group by post_id
order by post_id asc

# select * from t1