想象一个名为 的表Group,每个组都可以有“子”组,只要它们的“级别”较低。所以表之间是一对多的关系Group and childGroup。

和之间还有一个many-to-many关系Group,Items所以Groups_Items用来保存每个表的FK。
我需要编写一个查询,对于 a given Group key,我应该对、 和 all进行find all its child分组。all the childs of child groupsthe customers belonging to those found groups
我知道它需要是某种递归查询,但我不知道如何在 SQL 中完成。以下是讨论的表的结构:

所以如果我在上表中有这些数据集:

查询是 find For Group 1, find Its customers, Its Child groups (and their Childs) and all their customers,输出应该是:
<Group> 1
<customer> 1
<customer> 2
<Group> 2
<customer> 2
<Group> 3
<Group> 4
有人可以告诉我如何做到这一点吗?谢谢