2016-09-29 71 views
2

這裏是我的表結構:如何爲自動填充框選擇標籤?

// tags 
+----+------------+----------------------------------+----------+----------+------------+ 
| id | name |   description   | related | used_num | date_time | 
+----+------------+----------------------------------+----------+----------+------------+ 
| 1 | PHP  | some explanations for PHP  | 1  | 4234  | 1475028896 | 
| 2 | SQL  | some explanations for SQL  | 2  | 734  | 1475048601 | 
| 3 | jQuery  | some explanations for jQuery  | 3  | 434  | 1475068321 | 
| 4 | MySQL  | some explanations for MySQL  | 2  | 657  | 1475068332 | 
| 5 | JavaScript | some explanations for JavaScript | 3  | 3325  | 1475071430 | 
| 6 | HTML  | some explanations for HTML  | 6  | 2133  | 1475077842 | 
| 7 | postgresql | some explanations for postgresql | 2  | 43  | 1475077851 | 
| 8 | script  | some explanations for script  | 8  | 3  | 1475077935 | 
+----+------------+----------------------------------+----------+----------+------------+ 

現在我需要他們的名字的一部分選擇標籤的基礎上,我也需要選擇所有相關的標籤。


對於實施例:

字符串:scr。預期產出:

+----+------------+----------------------------------+----------+----------+------------+ 
| 3 | jQuery  | some explanations for jQuery  | 3  | 434  | 1475068321 | 
| 5 | JavaScript | some explanations for JavaScript | 3  | 3325  | 1475071430 | 
| 8 | script  | some explanations for script  | 8  | 3  | 1475077935 | 
+----+------------+----------------------------------+----------+----------+------------+ 
-- Noted that "jQuery" tag is selected because of its relation with "JavaScript" tag 

字符串:ph。預期產出:

+----+------------+----------------------------------+----------+----------+------------+ 
| 1 | PHP  | some explanations for PHP  | 1  | 4234  | 1475028896 | 
+----+------------+----------------------------------+----------+----------+------------+ 

字符串:ys。預期輸出:

+----+------------+----------------------------------+----------+----------+------------+ 
| 2 | SQL  | some explanations for SQL  | 2  | 734  | 1475048601 | 
| 4 | MySQL  | some explanations for MySQL  | 2  | 657  | 1475068332 | 
| 7 | postgresql | some explanations for postgresql | 2  | 43  | 1475077851 | 
+----+------------+----------------------------------+----------+----------+------------+ 
-- Noted that both "SQL" and "postgresql" are selected because of their relation with "MySQL" tag 

我怎麼能這樣做?

其實我是這樣做的:

SELECT * FROM tags WHERE name LIKE %:str% 

但我查詢不支持related列。

+0

@Drew是啊我知道,這不是你推薦的數據庫設計。 –

回答

1

加入表格本身並查找兩個名稱列中任何一個出現的行。這樣,您也可以搜索相關元素的名稱。這裏

SELECT t1.* 
FROM tags t1 
LEFT JOIN tags t2 ON t1.id = t2.related 
WHERE t1.name LIKE %:str% OR t2.name LIKE %:str%; 
1

一種選擇是自加入tags表,然後串聯組根據所輸入的標籤相關的標籤。以下查詢將輸出與給定輸入相關的標籤的CSV列表。例如,如果:str'MySQL'那麼輸出將是'MySQL,postgresql,SQL'。如果您使用的是像PHP或Java這樣的語言,應該很容易將這個CSV列表分解並將這些建議放入下拉列表中進行自動填充。

SELECT t1.name, 
     GROUP_CONCAT(t2.name) 
FROM tags t1 
INNER JOIN tags t2 
    ON t1.id = t2.related 
WHERE t1.name LIKE '%:str%'  -- e.g. MySQL 
GROUP BY t1.name 

你也可以做一個自聯接,並返回一個記錄每個建議的標籤:

SELECT t2.name 
FROM tags t1 
INNER JOIN tags t2 
    ON t1.id = t2.related 
WHERE t1.name LIKE '%:str%'  -- e.g. MySQL 
+0

爲什麼你的是一個完整的標籤名稱?它也適用於不全標籤名稱嗎?像'mys'或'ysql'或'ys'? –

+0

只有一件事,爲什麼不選擇'description'和'used_num'列?我試圖做一些這樣的事情:http://i.stack.imgur.com/vBjlH.png –

1

嘗試使用

SELECT * FROM tags a WHERE name LIKE %:str% or exist (SELECT id FROM tags b WHERE b.name LIKE %:str% and b.id=a.related) 
1

由於自動完成必須要快,你不能到在不止一個地方尋找答案。

構建(並維護)用於自動完成的特殊表格;它將有一個包含給定項目的所有可能關鍵字的文本(或varchar)列,以及關於它所指的項目的信息以及另一個列。

+0

聽起來不錯,upvote ..但真的很模糊..你能解釋我更多的解釋嗎?或表結構和查詢? –