【MySQL】group_concat 函数和 locate 函数运用之找到每篇文章的主题

力扣题

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

相关推荐

  1. Qt- 槽函数普通函数主要区别

    2024-03-23 04:42:03       24 阅读
  2. 【SAP HANA 34】HANA查找函数LOCATE使用

    2024-03-23 04:42:03       27 阅读
  3. 主题切换CSS文件

    2024-03-23 04:42:03       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-23 04:42:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-23 04:42:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-23 04:42:03       82 阅读
  4. Python语言-面向对象

    2024-03-23 04:42:03       91 阅读

热门阅读

  1. 第十四节 JDBC批量处理

    2024-03-23 04:42:03       45 阅读
  2. 什么是基于滤波SLAM算法和基于优化SLAM算法?

    2024-03-23 04:42:03       41 阅读
  3. mysql regex的介绍和用法

    2024-03-23 04:42:03       42 阅读
  4. 统计咨询|久菜盒子工作室可实现需求

    2024-03-23 04:42:03       41 阅读
  5. 赋能企业发展:亚信安慧AntDB的多维度支持

    2024-03-23 04:42:03       46 阅读
  6. vue3 + ts,如何获取路由传递的参数

    2024-03-23 04:42:03       40 阅读
  7. 制冷系统简单计算

    2024-03-23 04:42:03       37 阅读
  8. 推荐系统|冷启动问题解决方法

    2024-03-23 04:42:03       44 阅读