I have a taxonomies table like this:
+-----+--------------+----------------+--------------------+
| id | name | is_subcategory | parent_taxonomy_id |
+-----+--------------+----------------+--------------------+
| 80 | Headword | 0 | 0 |
| 81 | blonde | 1 | 80 |
| 82 | Parents | 0 | 0 |
| 83 | Children | 1 | 82 |
| 84 | Season | 0 | 0 |
| 85 | Winter | 1 | 84 |
| 86 | Charm Units | 0 | 0 |
| 88 | Etymology | 1 | 86 |
| 89 | Word History | 1 | 86 |
| 90 | Spring | 1 | 84 |
| 91 | Summer | 1 | 84 |
| 93 | Trends | 0 | 0 |
| 109 | Interest | 0 | 0 |
| 110 | Sports | 1 | 109 |
| 111 | Cups | 0 | 0 |
| 112 | hot | 1 | 111 |
| 113 | Speakers | 0 | 0 |
| 114 | Hi def | 1 | 113 |
| 115 | Dual powered | 1 | 113 |
| 118 | Office | 0 | 0 |
| 124 | States | 0 | 0 |
+-----+--------------+----------------+--------------------+
I need to fetch a list of all categories and subcategories from autocomplete, for example, if user types in sp
- he should get this list:
Interest/Sports
Season/Spring
Speakers
Speakers/Dual powered
Speakers/Hi def
Am I correct - I should do a self join? How would I get results in such format?
Edit:
here's my query that seems to work:
SELECT a.id AS cat_id, b.id AS subcat_id, a.name AS cat_name, b.name AS sub_cat_name, CONCAT_WS(' / ', a.name, b.name) AS full_name
FROM taxonomies as a
LEFT JOIN taxonomies AS b ON a.id = b.parent_taxonomy_id
WHERE (a.name LIKE 'sp%' OR b.name LIKE 'sp%')
ORDER BY full_name DESC;