M
M
Maxim2018-07-21 23:52:38
MySQL
Maxim, 2018-07-21 23:52:38

How to select categories and subcategories from the database?

base something like this
# name parent
1 services 0
2 car purchase 0
3 cars 25
4 hosting 30
5 construction 31
you need to display the name, parent 1, parent 2
can this be done in one request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-07-22
@xiiicool

sql solution
SELECT `c`.`id` as idpar1,`c`.`name` as parent1name,`c`.`parent` as parent1,`c2`.`id` as idpar2,`c2`.`name ` as parent2name,`c2`.`parent` as parent2, `c3`.`id`,`c3`.`name` as name,`c3`.`parent`
FROM `categories` `c` JOIN `categories` `c2` ON `c2`.`parent`=`c`.`id` JOIN `categories` `c3` ON `c3`.`parent`=`c2`.`id`
WHERE `c3`.`name` LIKE '%Lod%' LIMIT 50

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question