Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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搜索查询,按最佳匹配对结果排序_Mysql - Fatal编程技术网

MySQL搜索查询,按最佳匹配对结果排序

MySQL搜索查询,按最佳匹配对结果排序,mysql,Mysql,我正在一个网站中开发一个搜索引擎,访问者可以提供一些关键字,然后搜索引擎在数据表上执行一些查询 我知道如何查询一个表,但是如何构建一个查询来提供完整的结果列表和数据表中每个匹配行的出现次数 比如说,我有一个简单的表格,比如: id | title | desc ----------------- 1 | title 1 | some text with some text provding the user most likely no interesting info. 2 | title

我正在一个网站中开发一个搜索引擎,访问者可以提供一些关键字,然后搜索引擎在数据表上执行一些查询

我知道如何查询一个表,但是如何构建一个查询来提供完整的结果列表和数据表中每个匹配行的出现次数

比如说,我有一个简单的表格,比如:

id | title | desc
-----------------
1  | title 1 | some text with some text provding the user most likely no interesting info.

2 | title 2 | some other text

3 | title 3 | some third text saying some other crap.
我试图实现的是:当用户搜索“某些文本”时,我会得到如下结果:

id | title | desc | noOfMatches
-------------------------------
1  | title 1 | ... | 4

2  | title 3 | ... | 3

3  | title 2 | ... | 2
有人能帮我做这个吗?我不知道如何创建一个查询来计算查询中所提供单词的词组数…

也许这将帮助您:

也许这会帮助您:

使用
全文
搜索:

CREATE TABLE t_ft (id INT NOT NULL PRIMARY KEY, words VARCHAR(255) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET='utf8';

CREATE FULLTEXT INDEX fx_ft_words ON t_ft (words);
INSERT
INTO    t_ft (id, words)
VALUES  (1, 'some text with some text provding the user most likely no interesting info');

INSERT
INTO    t_ft (id, words)
VALUES  (2, 'some other text');

INSERT
INTO    t_ft (id, words)
VALUES  (3, 'some third text saying some other crap');

INSERT
INTO    t_ft
SELECT  id + 3, 'complete nonsense'
FROM    t_source
LIMIT   1000;

SELECT  *, MATCH(words) AGAINST('"some text"') AS relevance
FROM    t_ft
WHERE   MATCH(words) AGAINST('"some text"')
ORDER BY
        relevance DESC;

MATCH
将按
的顺序返回您可以使用的排名,使用
全文搜索:

CREATE TABLE t_ft (id INT NOT NULL PRIMARY KEY, words VARCHAR(255) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET='utf8';

CREATE FULLTEXT INDEX fx_ft_words ON t_ft (words);
INSERT
INTO    t_ft (id, words)
VALUES  (1, 'some text with some text provding the user most likely no interesting info');

INSERT
INTO    t_ft (id, words)
VALUES  (2, 'some other text');

INSERT
INTO    t_ft (id, words)
VALUES  (3, 'some third text saying some other crap');

INSERT
INTO    t_ft
SELECT  id + 3, 'complete nonsense'
FROM    t_source
LIMIT   1000;

SELECT  *, MATCH(words) AGAINST('"some text"') AS relevance
FROM    t_ft
WHERE   MATCH(words) AGAINST('"some text"')
ORDER BY
        relevance DESC;

MATCH
将返回您可以按
顺序使用的排名

谢谢,我做了一些更改,从您的示例中得到了我所需要的。谢谢,我做了一些更改,从您的示例中得到了我所需要的。