mysql - SQL get connection between ID's -


we have datatable:

id | parent_id  124    510  124    407  125    504  126    508 

i want result returns 407 child of 510:

id | childofparent_id | parent_id   124    407       510 125    null      504 126    null      508 

how can achive that? tried grouping id , counting distnict values no sucess.

child has lower id value: 4xx lower 5xx. i'm using mysql.

declare @t table (id int,parent_id int)  insert @t(id,parent_id) values (124,510),(124,407) ;with cte ( select id,parent_id,row_number()over(partition id order id)r @t) select t.id,t.parent_id,tt.parent_id childid  ( select id,parent_id cte r = 1)t,( select id,parent_id cte r = 2)tt 

Comments