PHP MySQL搜索标题和多个标记顺序

PHP MySQL搜索标题和多个标记顺序,php,mysql,search,full-text-search,Php,Mysql,Search,Full Text Search,我正在寻找最好的方法来提高我的网站的搜索功能 我有两个MySQL表“articles”和“tags” ->“文章”中的列 aid (auto increment) headline ..and a few more ->“标记”中的列(每个标记的新条目) 例如,如果我有一篇标题为“这是一篇绿色文章”的文章,而这篇文章的标签是“蓝色”、“黄色” 假设还有另一篇文章标题为“这是另一个测试”,标签为“蓝色”、“黄色”、“粉色”和“黑色” 如果一个访问者现在正在搜索“绿粉黑”,mysql应该会找到文章

我正在寻找最好的方法来提高我的网站的搜索功能

我有两个MySQL表“articles”和“tags”

->“文章”中的列

aid (auto increment)
headline
..and a few more
->“标记”中的列(每个标记的新条目)

例如,如果我有一篇标题为“这是一篇绿色文章”的文章,而这篇文章的标签是“蓝色”、“黄色”

假设还有另一篇文章标题为“这是另一个测试”,标签为“蓝色”、“黄色”、“粉色”和“黑色”

如果一个访问者现在正在搜索“绿粉黑”,mysql应该会找到文章“这是一篇绿色的文章”,还有另一篇文章,因为它的标签是“粉红”和“黑色”

此外,我需要一个函数,该函数根据文章的相关性对文章进行排序。 因此,在本例中,应首先显示文章“这是另一个测试”,因为如果标签“粉色”和“黑色”以及文章“这是一篇绿色文章”位于位置2(标题中只有“绿色”)

在过去的几个小时里,我尝试了一些查询,比如match..from和joins,但这对我来说太复杂了

有人给我一个提示吗


(很抱歉我的英语不好)

您的查询应该如下所示:

SELECT
    DISTINCT articles.aid
FROM articles
INNER JOIN tags
    ON articles.aid = tags.aid
WHERE tags.tag IN ("green", "pink", "black");
由于这两个表都有一个公共属性(即项目ID),因此可以使用内部联接来联接这两个表,并基于这两个表中的其他属性(在本例中为标记名)过滤结果

上面的查询将为您提供带有绿色、粉色或黑色标记的文章ID列表

编辑

抱歉,我没有看到您对相关性的要求。如果要了解在每篇文章中找到了多少个标记,请将其转换为分组查询,并计算找到的标记数:

SELECT
    articles.aid,
    articles.headline,
    COUNT(tags.tid)
FROM articles
INNER JOIN tags
    ON articles.aid = tags.aid
WHERE tags.tag IN ("green", "pink", "black")
GROUP BY articles.aid;
试试这个场景

首先,设置数据库:

mysql> CREATE TABLE articles (aid integer auto_increment, headline varchar(32), key(aid));
Query OK, 0 rows affected (0.13 sec)

mysql> CREATE TABLE tags (tid integer auto_increment, aid integer, tag VARCHAR(16), key(tid));
Query OK, 0 rows affected (0.09 sec)

mysql> INSERT INTO articles (headline) values ("This is a green Article"), ("This is another Test");
Query OK, 2 rows affected (0.05 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM articles;
+-----+-------------------------+
| aid | headline                |
+-----+-------------------------+
|   1 | This is a green Article |
|   2 | This is another Test    |
+-----+-------------------------+
2 rows in set (0.00 sec)

mysql> INSERT INTO tags (aid, tag) VALUES (1, "blue"), (1, "yellow"), (2, "blue"), (2, "yellow"), (2, "pink"), (2, "black");
Query OK, 6 rows affected (0.04 sec)
Records: 6  Duplicates: 0  Warnings: 0
然后运行一些查询:

mysql> SELECT articles.aid, articles.headline, COUNT(tags.tid) FROM articles INNER JOIN tags ON articles.aid = tags.aid WHERE tags.tag IN ("green", "pink", "black") GROUP BY articles.aid;
+-----+----------------------+-----------------+
| aid | headline             | COUNT(tags.tid) |
+-----+----------------------+-----------------+
|   2 | This is another Test |               2 |
+-----+----------------------+-----------------+
1 row in set (0.00 sec)

mysql> SELECT articles.aid, articles.headline, COUNT(tags.tid) FROM articles INNER JOIN tags ON articles.aid = tags.aid WHERE tags.tag IN ("green", "pink", "black", "yellow") GROUP BY articles.aid;
+-----+-------------------------+-----------------+
| aid | headline                | COUNT(tags.tid) |
+-----+-------------------------+-----------------+
|   1 | This is a green Article |               1 |
|   2 | This is another Test    |               3 |
+-----+-------------------------+-----------------+
2 rows in set (0.00 sec)

谢谢你的回复!这非常适合于标签。但我也想在文章标题中搜索。例如,搜索词是“绿色”的,没有“绿色”的标签,但是有一篇文章的标题中有“绿色”一词。(对不起,我的英语不好)
mysql> SELECT articles.aid, articles.headline, COUNT(tags.tid) FROM articles INNER JOIN tags ON articles.aid = tags.aid WHERE tags.tag IN ("green", "pink", "black") GROUP BY articles.aid;
+-----+----------------------+-----------------+
| aid | headline             | COUNT(tags.tid) |
+-----+----------------------+-----------------+
|   2 | This is another Test |               2 |
+-----+----------------------+-----------------+
1 row in set (0.00 sec)

mysql> SELECT articles.aid, articles.headline, COUNT(tags.tid) FROM articles INNER JOIN tags ON articles.aid = tags.aid WHERE tags.tag IN ("green", "pink", "black", "yellow") GROUP BY articles.aid;
+-----+-------------------------+-----------------+
| aid | headline                | COUNT(tags.tid) |
+-----+-------------------------+-----------------+
|   1 | This is a green Article |               1 |
|   2 | This is another Test    |               3 |
+-----+-------------------------+-----------------+
2 rows in set (0.00 sec)