2011-03-20 49 views
1

您好 我有一個選擇查詢存儲標籤和水平,需要得到各級標籤返回欄目

大問題有一個表:

tag_hierarhy 
-------------------- 
|id |parent_id | 
-------------------- 
|1  |0   | 
|2  |1   | 
|3  |1   | 
|3  |2   | 
|4  |1   | 
|4  |2   | 
|4  |3   | 
|5  |1   | 
|5  |2   | 
|5  |3   | 
-------------------- 
Comment: 
id  - this is the column with tag id 
parent_id - this is the id of the tag that is parent of tag with id from column id 
One tag can belong to many parents for example: tag 5 belongs to tag 1 and 2 and 3 

我有另一個表與我在哪裏保持水平的標籤。

tag_level 
-------------------- 
|id  |level | 
-------------------- 
|1  |0  | 
|2  |1  | 
|3  |2  | 
|4  |3  | 
|5  |3  | 
-------------------- 
Comment: 
id - this is the tag id 
level - this is the tag level in hierarhy 
0 - continent level, 
1 - country level, 
2 - region in country level, 
3 - city or village in level 2 

現在,我必須在一行中得到一個查詢層次。例如,對於使用id = 5的標籤,我想獲得一個一行以下數據:

------------------------------------------- 
| tid | l0_tid | l1_tid | l2_tid | l3_tid | 
------------------------------------------- 
| 5 | 1  | 2  | 3  | NULL | 
------------------------------------------- 
Comment: 
tid - the id of requested tag 
l0_tid - the id of the tag that tag with id=5 belongs to. This tag must be on level=0 
l1_tid - the id of the tag that tag with id=5 belongs to. This tag must be on level=1 
l2_tid - the id of the tag that tag with id=5 belongs to. This tag must be on level=2 
l3_tid - the id of the tag that tag with id=5 belongs to. Thie tag must be on level=3 
Imagine for Example tag 5 is pointing to 'Berlin' so: 
l0_tid must point to tag 'Europe', 
l1_tid must point to tag 'Germany'... and so on 

我不能改變的,因爲很多性能方面的考慮這種結構。任何人都可以幫我查詢mysql,以便能夠以我描述的方式獲取數據?我一整天都在爲此而戰,而我的思緒一片空白。我不希望子查詢只有連接。我正在尋求一些幫助。謝謝。表的

結構:

CREATE TABLE `street_traveler`.`tag_hierarhy` (
    `id` int(11) unsigned NOT NULL, 
    `parent_id` int(11) unsigned NOT NULL, 
    UNIQUE KEY `id_parent_id` (`id`,`parent_id`) 
) ENGINE=MyISAM 


CREATE TABLE `street_traveler`.`tag_level` (
    `id` int(11) unsigned NOT NULL, 
    `level` int(11) unsigned NOT NULL, 
    UNIQUE KEY `id_level` (`id`,`level`) 
) ENGINE=MyISAM 
+0

你能後你有什麼到目前爲止已經試過? – fretje 2011-03-20 15:22:41

回答

1

嘗試是這樣的:

SELECT t.id      AS tid, 
     GROUP_CONCAT(d0.parent_id) AS l0_tid, 
     GROUP_CONCAT(d1.parent_id) AS l1_tid, 
     GROUP_CONCAT(d2.parent_id) AS l2_tid, 
     GROUP_CONCAT(d3.parent_id) AS l3_tid 
FROM tag_level t 
     LEFT JOIN (SELECT th1.id, 
         th1.parent_id 
        FROM tag_hierarhy th1 
         JOIN tag_level t1 
          ON t1.id = th1.parent_id 
        WHERE level = 0 
        GROUP BY th1.id) d0 
     ON d0.id = t.id 
     LEFT JOIN (SELECT th2.id, 
         th2.parent_id 
        FROM tag_hierarhy th2 
         JOIN tag_level t2 
          ON t2.id = th2.parent_id 
        WHERE level = 1 
        GROUP BY th2.id) d1 
     ON d1.id = t.id 
     LEFT JOIN (SELECT th3.id, 
         th3.parent_id 
        FROM tag_hierarhy th3 
         JOIN tag_level t3 
          ON t3.id = th3.parent_id 
        WHERE level = 2 
        GROUP BY th3.id) d2 
     ON d2.id = t.id 
     LEFT JOIN (SELECT th4.id, 
         th4.parent_id 
        FROM tag_hierarhy th4 
         JOIN tag_level t4 
          ON t4.id = th4.parent_id 
        WHERE level = 3 
        GROUP BY th4.id) d3 
     ON d3.id = t.id 
GROUP BY t.id 

這將返回:

+---+------+------+------+------+ 
| 1 | NULL | NULL | NULL | NULL | 
| 2 | 1 | NULL | NULL | NULL | 
| 3 | 1 | 2 | NULL | NULL | 
| 4 | 1 | 2 | 3 | NULL | 
| 5 | 1 | 2 | 3 | NULL | 
+---+------+------+------+------+ 

你需要有

    指標
  • (tag_level.id,tag_level.level)
  • (tag_hierarchy.id,tag_hierarchy.parent_id)
+0

上述解決方案對我無效。此解決方案也有我想避免的子查詢。 – Ratownik 2011-03-20 16:14:26

+0

請再次檢查,我已更新查詢和輸出。上述表示中沒有子查詢只是加入派生表。這是有效的。 – Pentium10 2011-03-20 16:16:05

+0

非常感謝您的幫助,但是我在查詢瀏覽器或phpmyadmin中得到了列l * gid的結果blob。每個斑點都是NULL。索引存在:) – Ratownik 2011-03-20 16:25:03