我不确定这是不是你想要的,请告诉我:
我用重要的字段( ID、 模块、 模块子) 创建一个表格, 我填满了一些愚蠢的数据, 这就是我所拥有的。
mysql> show create table groupsG
*************************** 1. row ***************************
Table: groups
Create Table: CREATE TABLE `groups` (
`id` int(11) DEFAULT NULL,
`modu` varchar(200) DEFAULT NULL,
`sub_group` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> insert into groups (id,modu,sub_group) values (16, Module 1 ,null),
-> (19, Submodule 1 ,16),
-> (20, Submodule 2 , 16),
-> (22, Submodule 3 , 16),
-> (24, Module 2 ,null),
-> (25, Module 3 ,null),
-> (26, Submodule 4 , 25),
-> (27, Submodule 5 , 25),
-> (28, Submodule 6 , 25);
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> select * from groups;
+------+-------------+-----------+
| id | modu | sub_group |
+------+-------------+-----------+
| 16 | Module 1 | NULL |
| 19 | Submodule 1 | 16 |
| 20 | Submodule 2 | 16 |
| 22 | Submodule 3 | 16 |
| 24 | Module 2 | NULL |
| 25 | Module 3 | NULL |
| 26 | Submodule 4 | 25 |
| 27 | Submodule 5 | 25 |
| 28 | Submodule 6 | 25 |
+------+-------------+-----------+
9 rows in set (0.00 sec)
重要的事情是我应用了“自相残杀”的查询, 它看起来是这样的:
mysql> select * from groups as t1 join groups as t2 on t1.id=t2.sub_group;
+------+----------+-----------+------+-------------+-----------+
| id | modu | sub_group | id | modu | sub_group |
+------+----------+-----------+------+-------------+-----------+
| 16 | Module 1 | NULL | 19 | Submodule 1 | 16 |
| 16 | Module 1 | NULL | 20 | Submodule 2 | 16 |
| 16 | Module 1 | NULL | 22 | Submodule 3 | 16 |
| 25 | Module 3 | NULL | 26 | Submodule 4 | 25 |
| 25 | Module 3 | NULL | 27 | Submodule 5 | 25 |
| 25 | Module 3 | NULL | 28 | Submodule 6 | 25 |
+------+----------+-----------+------+-------------+-----------+
6 rows in set (0.00 sec)
如果您想要简化结果:
mysql> select t1.id as ID, t1.modu as PARENT, t2.modu as SON from groups as t1 join groups as t2 on t1.id=t2.sub_group;
+------+----------+-------------+
| ID | PARENT | SON |
+------+----------+-------------+
| 16 | Module 1 | Submodule 1 |
| 16 | Module 1 | Submodule 2 |
| 16 | Module 1 | Submodule 3 |
| 25 | Module 3 | Submodule 4 |
| 25 | Module 3 | Submodule 5 |
| 25 | Module 3 | Submodule 6 |
+------+----------+-------------+
6 rows in set (0.00 sec)
它只显示有孩子的模块:
如果您想要全部显示它们, 您可以做到这一点
mysql> select t1.id as ID, t1.modu as PARENT, t2.modu as SON from groups as t1 join groups as t2 on t1.id=IFNULL(t2.sub_group,t2.id);
+------+----------+-------------+
| ID | PARENT | SON |
+------+----------+-------------+
| 16 | Module 1 | Module 1 |
| 16 | Module 1 | Submodule 1 |
| 16 | Module 1 | Submodule 2 |
| 16 | Module 1 | Submodule 3 |
| 24 | Module 2 | Module 2 |
| 25 | Module 3 | Module 3 |
| 25 | Module 3 | Submodule 4 |
| 25 | Module 3 | Submodule 5 |
| 25 | Module 3 | Submodule 6 |
+------+----------+-------------+
9 rows in set (0.00 sec)
或以这种方式
mysql> select t1.id as ID, t1.modu as PARENT, if(t2.modu=t1.id,NULL,t2.sub_group) as SON from groups as t1 join groups as t2 on t1.id=IFNULL(t2.sub_group,t2.id);
+------+----------+------+
| ID | PARENT | SON |
+------+----------+------+
| 16 | Module 1 | NULL |
| 16 | Module 1 | 16 |
| 16 | Module 1 | 16 |
| 16 | Module 1 | 16 |
| 24 | Module 2 | NULL |
| 25 | Module 3 | NULL |
| 25 | Module 3 | 25 |
| 25 | Module 3 | 25 |
| 25 | Module 3 | 25 |
+------+----------+------+
9 rows in set, 9 warnings (0.00 sec)