"how to optimize mysql query getting categories and subcategories" Code Answer

3

try this query -

categories+sub categories:

select c1.id, c1.name, c2.id, c2.name from categories c1
  left join categories c2
    on c2.parent_id = c1.id
where c1.parent_id = 0

categories+sub categories+sub sub categories:

select c1.id, c1.name, c2.id, c2.name, c3.id, c3.name from categories c1
  left join categories c2
    on c2.parent_id = c1.id
  left join categories c3
    on c3.parent_id = c2.id
where c1.parent_id = 0
By mamoo on February 10 2022

Answers related to “how to optimize mysql query getting categories and subcategories”

Only authorized users can answer the Search term. Please sign in first, or register a free account.