Skip to content

Instantly share code, notes, and snippets.

@rodrigocorreaecastro
Last active September 23, 2022 12:01
Show Gist options
  • Select an option

  • Save rodrigocorreaecastro/abfc3f82eaf9e858ca0fc25e1634157e to your computer and use it in GitHub Desktop.

Select an option

Save rodrigocorreaecastro/abfc3f82eaf9e858ca0fc25e1634157e to your computer and use it in GitHub Desktop.
Category parent subcategory level
SELECT t1.id, t1.parent_id, t1.title,
( if(t4.id is null, 0,1) + if(t3.id is null, 0,1) + if(t2.id is null, 0,1) + if(t1.id is null, 0,1)) as level,
trim(leading '; ' from concat(ifnull(t4.title, ''), '; ', ifnull(t3.title, ''), '; ', ifnull(t2.title, ''), '; ', ifnull(t1.title, ''))) as path
FROM team t1
LEFT JOIN team t2 ON t1.parent_id=t2.id
LEFT JOIN team t3 ON t2.parent_id=t3.id
LEFT JOIN team t4 ON t3.parent_id=t4.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment