我不确定这是否是您想要的,请告诉我:
我创建了一个包含重要字段(id、模块、模块子)的表,并填写了一些愚蠢的数据,这就是我所拥有的。
mysql> show create table groups\G
*************************** 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)