sql - Multi Level Combox - MySQL Query -


i have mysql table looks enter image description here

and want create combobox looks this:

hardware (id 1, parent 0) -child (id 2, parent 1) -child (id 3, parent 1) --child (id 4, parent 3) 

as can see, main categories have parent id = 0, rest have id associated id of category.

what wondering, if there way this, directly through mysql query? if, so, can give me example?

i tried little loops in mysql couldn't make sense in head.

if limited 3 levels, can make 2 left joins , data in 1 query:

select     t1.id,     trim(         concat(             repeat('-',if(t2.id not null,1,0) + if(t3.id not null,1,0)),              ' ',              t1.label         )     ) label      mytable t1 left join     mytable t2 on (t1.parent = t2.id) left join     mytable t3 on (t2.parent = t3.id) order     ifnull(t3.id, ifnull(t2.id, t1.id)) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -