简体   繁体   中英

How to Get child of Parent in mysql?

I have a table below. I want to write mySql Query that can return all the children against the parent Id. ie against 0 it should return (1,2,3,4) and against 1 it should return (3,4).

I am able to do this recursive call in SQL server using "with" clause, but i want to do this same thing in MySQL.

ParentId GroupID
0 | 0
0 | 1
0 | 2
1 | 3
3 | 4

Any help in this regard is highly appreciated.

If this is an arbitrary depth hierarchy (you want ancestor instead of parent), then you can't do this directly in SQL. Google for "sql hierarchical queries" to find some possible workarounds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM