2012-08-29 45 views
0

我正在寻找最佳方法来改进我的网站的搜索功能。PHP MySQL搜索标题和多个标签订单

我有两个MySQL表 '文章' 和 '标签'

- >列在 '文章'

aid (auto increment) 
headline 
..and a few more 

- >列在 '标签'(新建项目对每个标记)

tid (auto increment) 
aid (ID from 'articles') 
tag 

例如,如果我与标题的文章“这是一个绿色条”和这篇文章的标签‘蓝’,‘黄’

,让我们说有另一文章,标题‘这又是一个测试’有以下标签‘蓝’ ,“黄色”,“粉红”“黑”

如果访问者正在搜寻“绿色粉色黑”的mysql应该在文章“这是一个绿色的文章”,也是其他文章,因为它的标签“粉红”“黑”

而且我需要它通过其相关定购物品的功能。 因此,在这个例子中,文章“这又是一个测试”应首先显示,因为如果标签“粉红”“黑”和文章“这是一个绿色的文章”是在两个位置(只标题中的“绿色”)

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

有没有人给我提示?

(对不起我的英文不好)

回答

2

您的查询应该是这样的:

SELECT 
    DISTINCT articles.aid 
FROM articles 
INNER JOIN tags 
    ON articles.aid = tags.aid 
WHERE tags.tag IN ("green", "pink", "black"); 

由于两个表有一个共同的属性(即文章ID),您可以使用一个内连接来加入两个表格,并根据两个表格中的其他属性(在本例中为标记名称)过滤结果。

上述查询将为您提供带有绿色,粉红色或黑色标记的文章ID列表。

编辑

对不起,我没有看到你的相关要求。如果您想了解有多少标签每篇文章被发现了,把它变成一个分组的查询,并找到COUNT的标签数量:

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) 
+0

谢谢您的回复!这适用于标签。但我也想在文章标题中进行搜索。例如,searchterm是“绿色”,没有标签是“绿色”,但是其中有一个文字标题“绿色”。 (对不起,我的英语不好) – Rob