mysql - 在mysql中搜索相似的词

标签 mysql sql full-text-search

我有一个数据库,其中有一个名为“药物”的表。其中有一个名为“描述”的字段,具有全文索引。我想搜索类似的词“Acifree -O 10ml”、“Acifree O 10ml”。

我尝试使用以下查询,

SELECT * FROM drugs where SOUNDEX(Description) = SOUNDEX('Acifree -O 10ml');

但是查询返回以下值

|ID  |Description          |
----------------------------
|177 |Acifree -O 10ml      |
|541 |Acifree O 10ml       |
|817 |Acifree -O 5ml       |
|817 |Acifree -O 7ml       |
|916 |Acifree -O 5 ml      |

我只需要“Acifree -O 10ml”和“Acifree O 10ml”值。就像下面这样

|ID  |Description          |
----------------------------
|177 |Acifree -O 10ml      |
|541 |Acifree O 10ml       |

有什么解决办法吗?

最佳答案

您可以尝试使用聚合函数,如下所示

select *, MATCH(Description) AGAINST('Acifree -O 10ml' IN NATURAL LANGUAGE MODE)
as score
from tutorial 
where MATCH(Description) AGAINST('Acifree -O 10ml' IN NATURAL LANGUAGE MODE)
and MATCH(Description) AGAINST('Acifree -O 10ml' IN NATURAL LANGUAGE MODE)=
(select  max(MATCH(Description) AGAINST('Acifree -O 10ml' IN NATURAL LANGUAGE MODE))
from tutorial
)

demo link

输出

id  description score
1   Acifree -O 10ml 0.15835624933242798
2   Acifree O 10ml  0.15835624933242798

当在 WHERE 子句中使用 MATCH() 时,返回的行会自动按照相关性最高的排在最前面。 相关性值是非负 float 。 零相关性意味着没有相似性。 相关性的计算基于 -

  • 行中的单词数
  • 该行中唯一单词的数量
  • 集合中的总字数
  • 文档数量 包含特定单词的(行)

由于您需要最相关的内容,所以我选择了最高分

关于mysql - 在mysql中搜索相似的词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54968443/

相关文章:

mysql - SQL - COUNT() 无法按预期工作

mysql - 按多列获取内容顺序

java - Hibernate Generated Value 策略

mysql - 查询 : has_many where all the child fields are nil

SQL 语句 - 查询一组具有最近日期的记录

postgresql - 使用同义词的 Postgres 全文搜索

MySQL:优化对记录范围的搜索。

php - 为什么 MATCH against 的效果不如 Exact match?

php - MySQL和PHP搜索相似词

mysql - 可移植 ODBC 连接驱动程序