Recursion is the most elegant way to do this, but I don't think mySql supports it in user-defined functions or stored procedures. I would suggest looping into a temp table or table to get your identifiers, and then join the table and request the results. I don't know mySql very well, so this is not verified, but there is something with this.
CREATE TEMPORARY TABLE tbl (myid int, ViewOrder int); Set @ifoundID=IdYourLookingFor; Set @ iStartID=@ifoundID ; Set @iOrder=0; INSERT INTO tbl(myid,ViewOrder)VALUES(@ifoundID,@iOrder); BEGIN
Hope that helps
source share