I have the following database schema
Course (id, fullname, shortname, category) Category (id, name , parent)
I want to make a select statement that concat all categories name of the course from leaf to top parent with id = 0
I have asked and some one advice me to use with recursive in mysql and he helped me with sample code to my problem and I have adjusted it to my schema
here is the code
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
when I run the query it does not give error, but it only show the leaf category and did not make any recursive or contamination
can anyone help please?
EDIT
CREATE TABLE `mdl_course` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`category` bigint(20) NOT NULL DEFAULT 0,
`sortorder` bigint(20) NOT NULL DEFAULT 0,
`fullname` varchar(254) NOT NULL DEFAULT '',
`shortname` varchar(255) NOT NULL DEFAULT ''
)
and for Course Category
CREATE TABLE `mdl_course_categories` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`idnumber` varchar(100) DEFAULT NULL,
`description` longtext DEFAULT NULL,
`descriptionformat` tinyint(4) NOT NULL DEFAULT 0,
`parent` bigint(20) NOT NULL DEFAULT 0
)

