Last active
August 29, 2015 14:14
-
-
Save haisum/e9be471853c3d57324f3 to your computer and use it in GitHub Desktop.
Query for getting All parents of a category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT T2.id, T2.title,T2.controller,T2.method,T2.url | |
FROM ( | |
SELECT | |
@r AS _id, | |
(SELECT @r := parent_id FROM menu WHERE id = _id) AS parent_id, | |
@l := @l + 1 AS lvl | |
FROM | |
(SELECT @r := 31, @l := 0) vars, | |
menu m | |
WHERE @r <> 0) T1 | |
JOIN menu T2 | |
ON T1._id = T2.id | |
ORDER BY T1.lvl DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fiddle: http://sqlfiddle.com/#!2/2eeb3/9