2012-08-29 43 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