I have two tables:
CREATE TABLE store_category (
store_id int NOT NULL PRIMARY KEY,
category_id char NOT NULL
)
CREATE TABLE category (
category_id char NOT NULL PRIMARY KEY,
parent_id char NULL,
name char NOT NULL
)
store_id | category_id |
---|---|
1 | 1a |
2 | 2b |
3 | 3c |
category
table: this table has 3 levels of category. The highest level of category has NULL
in parent_id
:
category_id | parent_id | name |
---|---|---|
1a | NULL | a |
2b | 1a | b |
3c | 2b | c |
I want to get all related category name of store category.
Expected query result:
store_id | category_id | lv1 | lv2 | lv3 |
---|---|---|---|---|
1 | 1a | a | b | c |
2 | 2b | a | b | c |
3 | 3c | a | b | c |
I have an idea, I will get all category names that relate to each category_id
in category
table into temp table and then join temp table with store_category
table. I wonder if that is a good idea.