1

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

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

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

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.

3
  • 1
    You need to write a stored procedure or do it in your programming language. It's not possible with a single SQL query in MySQL. Commented May 14, 2014 at 6:58
  • @vhu - i tried this but not getting results as i want. Commented May 14, 2014 at 7:02
  • I'm not saying that the answers in that question would solution to your problem, but just that it's a duplicate question. Commented May 14, 2014 at 7:04

2 Answers 2

3

What your expected output shows, can be achieved by using group_concat

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

fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

try this ,

SELECT p.id, GROUP_CONCAT(c.child) AS all_child_id FROM parent p left JOIN 
child c WHERE p.id=c.child GROUP BY p.id

1 Comment

there is single table -- not two tables

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.