I have two tables
Course with the following fields (ID, name, categoryID) Category with the following fields (ID, name, parentCategoryID)
for Example
Category
ID ------- name ------- parentCategoryID
1 -------- Category1 ----- 0
2 -------- Category2 ----- 1
3 -------- Category3 ----- 2
Course
ID ------- name ------- categoryID
1 -------- Course1 ----- 1
2 -------- Course2 ----- 2
3 -------- Course3 ----- 3
I want to write select statement to get the following result
Result
ID ------- name ------- category
1 -------- Course1 ----- Category1
2 -------- Course2 ----- Category2 - Category1
3 -------- Course3 ----- Category3 - Category2 - Category1
which means I want to get all category until root category with ID 0 and concatenate them to get course category name
can anyone help me please ?
EDIT
with recursive
n as (
select mc.id, mc.fullname , mc.shortname , mcc.name as category, mcc.parent
from mdl_course mc
join mdl_course_categories mcc on mcc.id = mc.category
union all
select n.id, n.fullname, n.shortname, concat(n.category, ' - ', mcc.name) as category , mcc.parent
from n
join mdl_course_categories mcc on mcc.id = n.parent
)
select id, fullname, shortname, category from n