php - To Get Structured Data mysql -
i need requirement below can please assist me depending on parent_id , id relation
id | category_name | parent_id 1 electronics 0 2 computer 1 3 ram 2 4 fashion 0 5 tshirt 4 6 round neck 5
i need requirement below
category name | sub category | sub sub category | electronics na na electronics computer na electronics computer ram fashion na na fashion tshirt na fashion tshirt round neck
please
after few hours of pain , suffering, arrived @ following query. note links given comments above did nothing me.
select t.* ( select coalesce(c3.category_name, c2.category_name, c1.category_name, 'na') `category name`, case when c3.category_name not null c2.category_name else case when c2.category_name not null c1.category_name else 'na' end end `sub category`, case when c3.category_name not null c1.category_name else 'na' end `sub sub category` category c1 left join category c2 on c1.parent_id = c2.id left join category c3 on c2.parent_id = c3.id ) t order t.`category name`, case when t.`sub category` = 'na' null else t.`sub category` end, case when t.`sub sub category` = 'na' null else t.`sub sub category` end
follow link below running demo:
Comments
Post a Comment