2013-06-27 177 views
0

反向查詢,我看到一篇文章 http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ 使用下面的數據從類別表的分層數據

category_id | name     | parent | 
+-------------+----------------------+--------+ 
|   1 | ELECTRONICS   | NULL | 
|   2 | TELEVISIONS   |  1 | 
|   3 | TUBE     |  2 | 
|   4 | LCD     |  2 | 
|   5 | PLASMA    |  2 | 
|   6 | PORTABLE ELECTRONICS |  1 | 
|   7 | MP3 PLAYERS   |  6 | 
|   8 | FLASH    |  7 | 
|   9 | CD PLAYERS   |  6 | 
|   10 | 2 WAY RADIOS   |  6 | 

在下面的查詢和下面的數據回報。

SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4 
FROM category AS t1 
LEFT JOIN category AS t2 ON t2.parent = t1.category_id 
LEFT JOIN category AS t3 ON t3.parent = t2.category_id 
LEFT JOIN category AS t4 ON t4.parent = t3.category_id 
WHERE t1.name = 'ELECTRONICS'; 

+-------------+----------------------+--------------+-------+ 
| lev1  | lev2     | lev3   | lev4 | 
+-------------+----------------------+--------------+-------+ 
| ELECTRONICS | TELEVISIONS   | TUBE   | NULL | 
| ELECTRONICS | TELEVISIONS   | LCD   | NULL | 
| ELECTRONICS | TELEVISIONS   | PLASMA  | NULL | 
| ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH | 
| ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS | NULL | 
| ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL | 
+-------------+----------------------+--------------+-------+ 
6 rows in set (0.00 sec) 

現在我的問題是,如果我有一個具有以下數據

+-------------+----------------------+--------------+-------+ 
    | lev1  | lev2     | lev3   | lev4 | 
    +-------------+----------------------+--------------+-------+ 
    | ELECTRONICS | TELEVISIONS   | TUBE   | NULL | 
    | ELECTRONICS | TELEVISIONS   | LCD   | NULL | 
    | ELECTRONICS | TELEVISIONS   | PLASMA  | NULL | 
    | ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH | 
    | ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS | NULL | 
    | ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL | 
    +-------------+----------------------+--------------+-------+ 

我如何可以使下面的數據返回反向查詢表或查詢:

category_id | name     | parent | 
    +-------------+----------------------+--------+ 
|   1 | ELECTRONICS   | NULL | 
|   2 | TELEVISIONS   |  1 | 
|   3 | TUBE     |  2 | 
|   4 | LCD     |  2 | 
|   5 | PLASMA    |  2 | 
|   6 | PORTABLE ELECTRONICS |  1 | 
|   7 | MP3 PLAYERS   |  6 | 
|   8 | FLASH    |  7 | 
|   9 | CD PLAYERS   |  6 | 
|   10 | 2 WAY RADIOS   |  6 | 

如果我在這個反向查詢中也可能具有等級字段(0或1或...)的值非常好。

category_id | name     | parent |position 

    +-------------+----------------------+--------+------- 
|   1 | ELECTRONICS   | NULL |0 
|   2 | TELEVISIONS   |  1 |0 
|   3 | TUBE     |  2 |3 
|   4 | LCD     |  2 |1 
|   5 | PLASMA    |  2 |2 

回答

0

ID從哪裏來?假設,你有每個項目的標識另一個表,則代碼可能類似於此:

select i.ID, l.lev1 as Name, NULL as Parent 
from IDTable i 
    join LevelTable l on i.Name = l.lev1 
union 
select i.ID, l.lev2 as Name, (select j.ID from IDTable j where j.Name = l.lev1) 
from IDTable i 
    join LevelTable l on i.Name = l.lev2 
union 
select i.ID, l.lev3 as Name, (select j.ID from IDTable j where j.Name = l.lev2) 
from IDTable i 
    join LevelTable l on i.Name = l.lev3 
+0

感謝它幫了我很多後10天搜索了很多論壇,現在只要我有2個問題: 1-關於(假設,你有另一個表每個項目的ID)你的意思是我必須定義新的表與自動編號ID,項目,然後插入所有級別的所有項目與獨特的關鍵詞。 2-我如何在查詢中定位字段。例如TUBE,LCD,PLASMA的父母現在是TELEVISIONS我需要按照字母順序給出每個位置(0,1,2,3 ...)的值的位置字段值。 – masoud