【问题标题】:Search a word and Get Predictive Search Words from String (not complete string) in Mysql在 Mysql 中搜索一个单词并从字符串(不完整的字符串)中获取预测搜索词
【发布时间】:2021-03-31 13:15:49
【问题描述】:

我有如下所列的 SQL 表设计

*-----------------------------------------
| id | title                              |
| -- | -----------------------------------|
| 1  | This is nice pen looking good      |
| 2  | This is nice pen looking elegent   |
| 3  | This is nice pen looking great     |
| 4  | This is nice pen looking best.     |
------------------------------------------

示例查询:Select * from table where title LIKE '%looking%'

当我尝试使用 (like query) 或使用 (Regular Expressions) 例如示例查询来搜索词 "looking" 时,我获得下面提到的完整字符串结果

结果

*------------------------------------
| title                              |
| -----------------------------------|
| This is nice pen looking good      |
| This is nice pen looking elegent   |
| This is nice pen looking great     |
| This is nice pen looking best.     |
-------------------------------------

我想要什么?

我想要预测性搜索词(不是完整的字符串)

我如何通过使用 SQL 搜索单词(查找)来获得以下提到的结果。

*-------------------
| title             |
| ------------------|
| looking good      |
| looking elegent   |
| looking great     |
| looking best.     |
--------------------

请建议我如何编写查询以获得这些类型的结果? 谢谢

【问题讨论】:

  • 请解释一下所说的“预测性搜索词”是什么意思。
  • 表示当我尝试搜索单词“looking”时,我想要精确匹配单词的结果和搜索单词的下一个单词。搜索词:“寻找”结果我想要“看起来不错”“看起来优雅”“看起来很棒”等
  • 一个有趣的问题,但错误的工具集。 Mysql 是一个数据库,而不是一个文本分析工具。它没有词的概念。你显然可以组合一组字符串函数,并在 mysql 中实现你想要的紧密代理,但性能会很糟糕。

标签: mysql sql search full-text-search exact-match


【解决方案1】:

你可以使用substring_index()获得下一个作品:

Select t.*,
       substring_index(substring_index(concat(' ', title, ' '), ' looking ', -1), ' ', 1) as next-word
from table
where title LIKE '%looking%';

Here 是一个 dbfiddle。

【讨论】:

  • 当这个解决方案不适用于现实生活中的文本时,我可以给你几个案例。你过于简单化了。
猜你喜欢
  • 1970-01-01
  • 2013-09-15
  • 2011-12-27
  • 2019-02-12
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多