Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 比较两个url段塞以查找相同单词的计数_Mysql - Fatal编程技术网

Mysql 比较两个url段塞以查找相同单词的计数

Mysql 比较两个url段塞以查找相同单词的计数,mysql,Mysql,我想在我的网站上找到类似的帖子,这取决于url slug。 假设我有以下五个鼻涕虫 i-am-a-slug /*the slug i want to compare*/ i-am-another-slug /* 3 same words */ i-am-an-ant /* 2 same words */ the-slug-life /* 1 same word */ foo-bar /* 0 same words *

我想在我的网站上找到类似的帖子,这取决于url slug。 假设我有以下五个鼻涕虫

i-am-a-slug         /*the slug i want to compare*/
i-am-another-slug   /* 3 same words */
i-am-an-ant         /* 2 same words */    
the-slug-life       /* 1 same word */
foo-bar             /* 0 same words */
目前,我正在使用下面的代码来找出比较的slug中是否有任何类似的单词

SELECT *
FROM News
WHERE News.slug != "i-am-a-slug"
ORDER BY CASE
WHEN News.slug REGEXP "i|am|a|slug" THEN 1
ELSE 2
它甚至不能很好地工作。。。示例中的
a
这样的词几乎会在我数据库中的每个slug中返回命中率。。。在本例中,即使是slug
foo-bar
也会返回

我似乎不知道如何选择一个变量
same words count
,该变量对每个测试slug中的所有相同单词进行计数(有关我想要得到的解决方案,请参阅注释第一个代码块),因此我可以

SELECT *
FROM News
WHERE News.slug != "i-am-a-slug"
ORDER BY CASE
WHEN same-words-count > 2 THEN 1
WHEN same-words-count = 2 THEN 2
WHEN same-words-count = 1 THEN 3
ELSE 4
还是有更好的方法


非常感谢您,很抱歉我的mysql最近有点生锈…

您正在寻找的是所谓的反向索引:


根据数据量和您对结果的实际操作(是否需要保持最新,是否需要在网站上显示等),您可能希望使用您选择的编程语言或使用一些成熟的全文搜索解决方案来解决问题。简单的SQL不是一个合适的工具。

我只想在我的网站上显示类似的URI。我可能会选择所有的slug,然后使用php进行排序。。。非常感谢。