I have the following table and would like to write a proper SQL query in MariaDB / mySQL to return the results underneath.
"dirID" "parentID" "name"
"1" "0" "C:\"
"2" "1" "\temp"
"3" "1" "\Users"
"4" "3" "\Jon"
dirID name
1 C:
2 C:\temp
3 C:\Users
4 C:\Users\John
So far I am trying to use CASE WHEN which I am pretty sure is way too inefficient and not the solution to the problem as follows:
cDir being the child dir and pDir being the parent:
SELECT
cDir.dirID,
cDir.parentID,
cDir.name AS name,
CASE
WHEN cDir.parentID != 0 THEN ( SELECT pDir.name )
END AS path
FROM dirs AS cDir
JOIN dirs AS pDir ON cDir.parentID = pDir.dirID
So at the end I want to do a CONCAT.
Any help? Thanks.
MariaDB and MySQL do not have a hierarchical/recursive query, but you can do a query with limited levels.
This is a query handling up to nine levels:
http://sqlfiddle.com/#!2/3b1c70/1