有没有一种方法可以对以单数和复数形式找到的数据表中的任何单词进行mySQL搜索?

问题描述

所以我的数据库中大约有3000篇文章,有些看起来太相似或使用带有复数的标题。第一个问题是我没有话要注意。其次,我需要建立这些术语的列表,以使我知道可以重新考虑并删除或合并的内容。我最好奇的部分是获得复数和单数形式的单词的任何迭代。一些可能的单词集的示例:

  • 抽象背景
  • 抽象背景
  • 抽象桌面背景
  • 抽象桌面背景
  • 抽象的移动背景
  • 抽象的手机背景

等...

我拼凑了以下内容-但找不到任何未提供的单词...所以我希望它是一个变量...

SELECT post_title,post_type,url FROM posts WHERE REGEXP '[[:<:]]background[[:>:]]*' AND post_type='published'ORDER BY post_title ASC;

解决方法

您是否已检查MySQL手册进行全文搜索?

请参阅:https://dev.mysql.com/doc/refman/8.0/en/fulltext-natural-language.html

自然语言全文查询示例:

获取针对关键字的列文本得分

mysql> SELECT id,MATCH (title,body)
    AGAINST ('Tutorial' IN NATURAL LANGUAGE MODE) AS score
    FROM articles;
+----+---------------------+
| id | score               |
+----+---------------------+
|  1 | 0.22764469683170319 |
|  2 |                   0 |
|  3 | 0.22764469683170319 |
|  4 |                   0 |
|  5 |                   0 |
|  6 |                   0 |
+----+---------------------+
6 rows in set (0.00 sec)

在选择列表和where子句中使用MATCH()表达式

mysql> SELECT id,body,body) AGAINST
    ('Security implications of running MySQL as root'
    IN NATURAL LANGUAGE MODE) AS score
    FROM articles WHERE MATCH (title,body) AGAINST
    ('Security implications of running MySQL as root'
    IN NATURAL LANGUAGE MODE);
+----+-------------------------------------+-----------------+
| id | body                                | score           |
+----+-------------------------------------+-----------------+
|  4 | 1. Never run mysqld as root. 2. ... | 1.5219271183014 |
|  6 | When configured properly,MySQL ... | 1.3114095926285 |
+----+-------------------------------------+-----------------+
2 rows in set (0.00 sec)
,

REGEX在语法上非常讲究,它会在背景前加上一个空格。在此之前和之后,您可以输入任何类型的文本。

如果您想要更具体的文本模式,则必须找到确切的语法

CREATE TABLE posts
    (`post_title` varchar(28))
;
    
INSERT INTO posts
    (`post_title`)
VALUES
    ('abstract background'),('abstract backgrounds'),('abstract desktop backgrounds'),('abstract desktop background'),('abstract mobile background'),('abstract mobile backgrounds')
;
✓

✓
SELECT post_title FROM posts WHERE post_title REGEXP 'abstract .*background.*';
| post_title                   |
| :--------------------------- |
| abstract background          |
| abstract backgrounds         |
| abstract desktop backgrounds |
| abstract desktop background  |
| abstract mobile background   |
| abstract mobile backgrounds  |

* db 提琴here fiddle = cd40c8103ae36b2fa3f327a9bcf178c3)