it has been a time for me since doing SQL, so I just want to ask for help with this one.
I would like a solution without temp table if possible, but better than nothing ofc, would be glad for every solution! Not really sure if this is possible without it...
I have two tables.
Table Premise
| premise_id | category_id |
|---|---|
| 1 | 5 |
| 2 | 8 |
| 3 | 20 |
| 4 | 6 |
Table Category - this is just slice coresponding to the above premise_id 3. I need for every premise to get it's category AND all of it's parents.
| id | parent_id | name |
|---|---|---|
| 20 | 17 | nameA |
| 17 | 11 | nameB |
| 11 | 5 | nameC |
| 5 | null | nameD |
The RESULT should look like this:
| premise_id | category_id | category_name |
|---|---|---|
| 3 | 20 | nameA |
| 3 | 17 | nameB |
| 3 | 11 | nameC |
| 3 | 5 | nameD |
All I managed to write is this recursive query for getting the tree inside category:
with recursive categories as (
select id, id_path, parent_id, name from category
union all
select c.id, c.id_path, c.parent_id, c.name from category c
join categories ctgs on ctgs.parent_id = c.id
) select * from categories
;
Getting lazy, will do prtscreens now. It results fine like this. There IS the parents-path itself, as id_path, but would not like to rely on that. Worth noting is that not always the children has higher ID than parent !
---------- But I am not really sure how to join it. Feels like I am missing something really obvious, but really, what is the join condition on this? Or should I go with temp table? I am using Mariadb 10.5.
Thank you for your time guys.
