Mysql递归查询——获取所有父子关系 [英] Mysql Recursive Query - get all parent-child relationship

查看:860
本文介绍了Mysql递归查询——获取所有父子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含以下数据的表格.

I have table with below data.

id|child
1|2
1|3
1|4
2|6
2|7
3|9
6|10
6|11
9|12
9|13
10|14
10|15
17|18
19|20

我需要获取如下数据.即一个id的所有父子关系.

I need to fetch the data like below. i.e all parent child relationship for a id.

这里我递归地选择了所有孩子的父ID1"

Here I have select all childs recursively for parent id "1"

id|all_child_id
1|2,3,4
2|6,7
3|9
6|10,11
9|12,13
10|14,15

我正在寻找 Mysql 不支持的WITH RECURSIVE".有人可以帮助我如何编写这样的递归查询,其中深度不能是任何约束.

I am looking for "WITH RECURSIVE" which is not supported my Mysql. Can someone help me how to write such recursive query where depth must not be any constraint.

推荐答案

使用group_concat

select parent, group_concat(child)
from table
group by parent

小提琴

这篇关于Mysql递归查询——获取所有父子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆