mysql select with while loop
问题 I would like to select data from my table and using one of the columns, create a loop to define additional data. For example: 'select id,related_id,name from ancestors' id, related_id, name 1, 0, Bob 2, 1, Dave 3, 2, Susie 4, 1, Luke 5, 0, Cindy 6, 5, Sam Bob is the grandfather, Dave and Luke are his children and Susie is his granddaughter. Cindy has a child Sam. Now, I want to use related_id to figure out how many levels the ancestor tree goes down. So I want the results to be: id, related