My category table is as follows:
------------------------------------ id | name | parent_id | ------------------------------------ 1 | Vehicles | 0 | 2 | Car Insurance | 1 | 3 | Van Insurance | 1 | 4 | PhoneRecharge | 0 | 5 | prepaid | 4 | 6 | postpaid | 4 |
The result should look like this:
--------------------------------------------------------- id | parent_id | main_category_name | sub_category_name| --------------------------------------------------------- 2 | 1 | Vehicles | Car Insurance | 3 | 1 | Vehicles | Van Insurance | 5 | 4 | PhoneRecharge | prepaid | 6 | 4 | PhoneRecharge | postpaid |
To get the above record, I need to minimize the interaction with the database. Therefore, I need to achieve this above data in one query.
source share