Created
September 15, 2010 10:48
-
-
Save jayniz/580550 to your computer and use it in GitHub Desktop.
Nested set indented tree output in SQL
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
-- Indented output, like a tree | |
SELECT CONCAT(REPEAT(' ', count(*)-1), node1.name) | |
FROM genres AS node1, | |
genres AS node2 | |
WHERE node1.lft BETWEEN node2.lft AND node2.rgt | |
GROUP BY node1.LFT; | |
-- Unindented but with level (for corrupt data) | |
SELECT node1.name, COUNT(*) AS level | |
FROM genres AS node1, | |
genres AS node2 | |
WHERE node1.lft BETWEEN node2.lft AND node2.rgt | |
GROUP BY node1.LFT; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment