服务器怎么发布网站有没有在家做的兼职网站
力扣题
1、题目地址
2199. 找到每篇文章的主题
2、模拟表
表:Keywords
| Column Name | Type | 
|---|---|
| topic_id | int | 
| word | varchar | 
- (topic_id, word) 是该表的主键(具有唯一值的列的组合)。
 - 该表的每一行都包含一个主题的 id 和一个用于表达该主题的词。
 - 可以用多个词来表达同一个主题,也可以用一个词来表达多个主题。
 
表:Posts
| Column Name | Type | 
|---|---|
| post_id | int | 
| content | varchar | 
- post_id 是该表的主键(具有唯一值的列)。
 - 该表的每一行都包含一个帖子的 ID 及其内容。
 - 内容仅由英文字母和空格组成。
 
3、要求
Leetcode 从其社交媒体网站上收集了一些帖子,并对每个帖子的主题感兴趣。
每个主题可以由一个或多个关键字表示。
如果某个主题的关键字存在于一个帖子的内容中 (不区分大小写),那么这个帖子就有这个主题。
编写解决方案,根据以下规则查找每篇文章的主题:
 1、如果帖子没有来自任何主题的关键词,那么它的主题应该是 “Ambiguous!”。
 2、如果该帖子至少有一个主题的关键字,其主题应该是其主题的 id 按升序排列并以逗号 ‘,’ 分隔的字符串。字符串不应该包含重复的 id。
 以 任意顺序 返回结果表。
4、示例
输入:
Keywords 表:
| topic_id | word | 
|---|---|
| 1 | handball | 
| 1 | football | 
| 3 | WAR | 
| 2 | Vaccine | 
Posts 表:
| post_id | content | 
|---|---|
| 1 | We call it soccer They call it football hahaha | 
| 2 | Americans prefer basketball while Europeans love handball and football | 
| 3 | stop the war and play handball | 
| 4 | warning I planted some flowers this morning and then got vaccinated | 
输出:
| post_id | topic | 
|---|---|
| 1 | 1 | 
| 2 | 1 | 
| 3 | 1,3 | 
| 4 | Ambiguous! | 
解释:
1:“We call it soccer They call it football hahaha”
 “football” 表示主题 1。没有其他词能表示任何其他主题。
2:“Americans prefer basketball while Europeans love handball and football”
 “handball” 表示主题 1。“football” 表示主题 1。
 没有其他词能表示任何其他主题。
3:“stop the war and play handball”
 “war” 表示主题 3。 “handball” 表示主题 1。
 没有其他词能表示任何其他主题。
4:“warning I planted some flowers this morning and then got vaccinated”
 这个句子里没有一个词能表示任何主题。注意 “warning” 和 “war” 不同,尽管它们有一个共同的前缀。
 所以这篇文章 “Ambiguous!”
 请注意,可以使用一个词来表达多个主题。
5、代码编写
知识点
group_concat 用法可参考我以前文章
【MySQL】CONCAT、CONCAT_WS、GROUP_CONCAT 函数用法
字符串函数 locate 用法
语法:locate(substr,str)
 作用:用于返回 str 中 substr 所在的位置索引,如果找到了,则返回一个大于0的数,否则返回0。
 例子:比如在 table 表里有个字段名 field 值为 “I like playing”,如果要将这个字段包含“like”的查询出来,可以用
 select * from table where locate('like', field) > 0
我的代码
里面比较关键的一点是需要对匹配的字符串和被匹配的字符串前后都加空格,原因:
 1、对匹配的字符串前后加空格,防止错误匹配(war -> warning)
 2、对被匹配的字符串前后加空格,防止前后匹配不到(’ handball ’ -> 'handball ')
select one.post_id, ifnull(group_concat(distinct topic_id order by topic_id separator ','), 'Ambiguous!') AS topic
from Posts one
left join Keywords two on locate(concat(' ', two.word, ' '), concat(' ', one.content, ' ')) > 0
group by 1
