sql - Parents tree in mysql table (while-loop) -


sorry english. have mysql table this

[ --------------------------] [ parent_id ] [ category_id ] [ --------------------------] 

site structured this:

0 -> 1 -> 2 -> -> 3 -> -> -> 5 -> 4 

and table like

0 1 0 2 2 3 0 4 3 5 

how write mysql while loop input 5 , list of it's parents until 0:

3 2 

i know, how write in php, want 1 query database, when try run "while" examples official manuals, returns lot of errors.

you can achieve procedures..

create procedure `root_connect`(in init char(1),out str char(15)) begin     set @startchar:=(select category_id tablename parent_id = init);     set @endloop := "no";     set @fullchar:= @startchar;     set @newchar:= "";       if (@startchar !="-" or @startchar =null)          while (@endloop = "no")                               set @newchar :=(select category_id tablename parent_id = @startchar);                    if(@newchar = '-')                 set @endloop := "yes";             else                 set @fullchar:= concat(@fullchar,"-",@newchar);             end if;                      set @startchar := @newchar;              end while;     end if;         select @fullchar; end 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -