Hierarchal data management (MySQL)

From Wiki @ Karl Jones dot com
Revision as of 08:48, 19 February 2016 by Karl Jones (Talk | contribs) (Created page with "This article discusses hierarchal data management for MySQL. == Select full path to leaf == <pre> SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name a...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This article discusses hierarchal data management for MySQL.

Select full path to leaf

SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
LEFT JOIN category AS t4 ON t4.parent = t3.category_id
WHERE t1.name = 'ELECTRONICS' AND t4.name = 'FLASH';

Source: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

See also