0

i have a table, Categories, it has a column 'id' (PK) and the same table has the column parent which is a FK. Table

What i need (and can't) is to take the 'parent' id and show the value 'name' associated to this id, when i select 'name' it doesn't show me 'parent' name but 'id' name.

Structure

Please let me know if the information is enough.

Thanks for your time.

Juan_P
  • 3
  • 1

1 Answers1

0
SELECT c.id AS courseid, c.fullname AS coursename,
    cat.id AS catid, cat.name AS catname,
    cat.parent AS parentcatid,
    CASE
        WHEN cat.parent = 0 THEN 'Root Category'
        ELSE parentcat.name
    END AS parentcatname
FROM mdl_course c
JOIN mdl_course_categories cat ON cat.id = c.category
LEFT JOIN mdl_course_categories parentcat ON parentcat.id = cat.parent
Russell England
  • 9,436
  • 1
  • 27
  • 41