Skip to content

Instantly share code, notes, and snippets.

@jayniz
Created September 15, 2010 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayniz/580550 to your computer and use it in GitHub Desktop.
Save jayniz/580550 to your computer and use it in GitHub Desktop.
Nested set indented tree output in SQL
-- 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