recursive-cte

Select statement to return parent and infinite children

扶醉桌前 提交于 2019-11-26 14:42:18
问题 Give the table structure, as something like: ID ParentID Name 1 NULL A root 2 NULL Another root 3 1 Child of 1 4 3 Grandchild of 1 5 4 Great grandchild of 1 6 1 Child of 1 7 NULL Another root 8 7 Child of 6 I am looking for an elegant (if possible) solution for a single Sql statement/function that would return all data in the table when given an ID = 1 So my result would look something like: ID ParentID Name 1 NULL A root 3 1 Child of 1 4 3 Grandchild of 1 5 4 Great grandchild of 1 6 1 Child