Skip to content

Instantly share code, notes, and snippets.

@jonbernardi
Created April 14, 2016 06:59
Show Gist options
  • Select an option

  • Save jonbernardi/f40e4cf25847e41cc666320500019c5b to your computer and use it in GitHub Desktop.

Select an option

Save jonbernardi/f40e4cf25847e41cc666320500019c5b to your computer and use it in GitHub Desktop.
Get breadcrumbs for nested set
SELECT
GROUP_CONCAT(
ancestor.name
ORDER BY ancestor.lft ASC
SEPARATOR ' > '
),
child.*
FROM `categories` child
JOIN `categories` ancestor
ON child.lft >= ancestor.lft
AND child.lft <= ancestor.rgt
GROUP BY child.lft
ORDER BY child.lft
@barokurniawan

Copy link
Copy Markdown

I learn new things from this SQL, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment