sql - Select all parents records in mysql -
table structure:
id(int) title(varchar) parent(int) 1 accessories 0 2 man 1 3 women 1 4 watches 2 5 new watches 4 6 used watches 5
suppose if on forth or fifth level children category, how retrieve parent categories in query. want show breadcrumbs hierarchy.
you can try with:
select @start := id 'id', title, parent table1 join (select @start := 0) temp parent = @start , id <= 4;
note without id < 4
check, you'll full tree "top" (0
) "bottom" (6
).
checkout sqlfiddle, too.
Comments
Post a Comment