修复了3级菜单树

时间:2016-03-08 17:01:33

标签: mysql

我需要一个3级菜单树(不是无限制)

tablename = LEVEL1

+-------------+----------------------+
| level1_id   | name                 |
+-------------+----------------------+
|           1 | AIR CONDITIONER      |
|           2 | TV                   |
+-------------+----------------------+

tablename = LEVEL2

+-------------+----------------------+-----------+
| level2_id   | name                 | level1_id |
+-------------+----------------------+-----------+
|           1 | SPLIT TYPE           |      1    |
|           2 | WINDOWS TYPE         |      1    |
|           3 | LCD                  |      2    |
|           4 | PLASMA               |      2    |
|           5 | MUSIC PLAYER         |   NULL    |
+-------------+----------------------+-----------+

tablename = LEVEL3

+-------------+----------------------+-----------+
| level3_id   | name                 | level2_id |
+-------------+----------------------+-----------+
|           1 | AC 0.75 TON          |      1    |
|           2 | AC 1 TON             |      1    |
|           3 | WT 0.75 TON          |      2    |
|           4 | WT 1 TON             |      2    |
|           5 | LCD 32" UP           |      3    |
|           6 | LCD 42" UP           |      3    |
|           7 | PLASMA 32" UP        |      4    |
|           8 | PLASMA 42" UP        |      4    |
|           9 | CD PLAYERS           |      5    |
|          10 | MP3 PLAYERS          |      5    |
|          11 | CDS                  |   NULL    |
|          12 | BOOKS                |   NULL    |
+-------------+----------------------+-----------+

是否可以运行一个获得以下结果的mysqli查询? 这样我就知道每个类别究竟属于哪个级别?

+------------------+----------------+-----------------+
| LEVEL1           | LEVEL2         | LEVEL3          |
+------------------+----------------+-----------------+
| AIR CONDITIONER  | SPLIT TYPE     | AC 0.75 TON     |
| AIR CONDITIONER  | SPLIT TYPE     | AC 1 TON        |
| AIR CONDITIONER  | WINDOWS TYPE   | WT 0.75 TON     |
| AIR CONDITIONER  | WINDOWS TYPE   | WT 1 TON        |
| TV               | LCD            | LCD 32" UP      |
| TV               | LCD            | LCD 42" UP      |
| TV               | PLASMA         | PLASMA 32" UP   |
| TV               | PLASMA         | PLASMA 42" UP   |
| NULL             | MUSIC PLAYER   | CD PLAYERS      |
| NULL             | MUSIC PLAYER   | MP3 PLAYERS     |
| NULL             | NULL           | CDS             |
| NULL             | NULL           | BOOKS           |
+------------------+----------------+-----------------+

有点贪婪。有人可以帮我这个,列出所有的名称/ ID WHERE父ID是空的

+------------------+------------+------+
| name             | tablename  |  id  |
+------------------+------------+------+
| AIR CONDITIONER  | LEVEL1     |   1  |
| TV               | LEVEL1     |   2  |
| MUSIC PLAYER     | LEVEL2     |   5  |
| CDS              | LEVEL3     |  11  |
| BOOKS            | LEVEL3     |  12  |
+------------------+------------+------+

1 个答案:

答案 0 :(得分:1)

正如您所呈现的那样,Level3是主表,并且总是会有一些数据。我想你的方式符合你的需要。但是你应该尝试使用两个表,Id,Name,ParentID只有一个表可以无限制关系。其中Parentid是同一个表的更高级别的id。

无论如何,这个查询应该适合您的情况:

' SELECT LEVEL1.name,LEVEL2.name,LEVEL3.name FROM(LEVEL1 RIGHT JOIN LEVEL2 ON LEVEL1.level1_id = LEVEL2.level1_id)RIGHT JOIN LEVEL3 ON LEVEL2.level2_id = LEVEL3.level2_id;'