您好 我有一個選擇查詢存儲標籤和水平,需要得到各級標籤返回欄目
大問題有一個表:
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
你能後你有什麼到目前爲止已經試過? – fretje 2011-03-20 15:22:41