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
4

1 回答 1

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
于 2013-06-27T06:19:53.920 回答