MagpieDean
MagpieDean

Reputation: 1

How can I add different node names as columns in a recursive query MySQL 8

I have a self-referencing Category table in MySQL 8 and I am able to successfully extract each level in the hierarchy with this query.

with recursive cat_path 
        (
            CategoryID, CategoryName, ParentCategoryID, 
            InheritCatAttrID, InheritCatTextID, InheritCatImageID, InheritCatTemplateID, 
            AttSchemaID, CatTextSchemaID, TemplateID, TemplateName, 
            SortOrder, IsRoot, IsCollection, IsPanel, IsStaticContent,
            PageNumber, GridxPosition, GridyPosition, GridWidth, GridHeight, BlockSizeID, BlockxPosition, BlockyPosition, BlockWidth, BlockHeight, ColourCMYK,
            sortpath, CategoryLevel, BlockSizeName, CatCount, ProdCount
        ) as 
        (
            select 
                CategoryID, CategoryName, ParentCategoryID, 
                InheritCatAttrID, InheritCatTextID, InheritCatImageID, InheritCatTemplateID, 
                (select AttributeSchemaID from Category c2 WHERE c2.CategoryID = Category.InheritCatAttrID) as AttSchemaID,  
                (select CategoryTextSchemaID from Category c2 WHERE c2.CategoryID = Category.InheritCatTextID) as CatTextSchemaID,  
                (select CategoryTemplateID from Category c2 WHERE c2.CategoryID = Category.InheritCatTemplateID) as TemplateID, 
                (select TemplateName from CategoryTemplate ct WHERE CategoryTemplateID = TemplateID) as TemplateName, 
                IFNULL(SortOrder,0), IsRoot, IsCollection, IsPanel, IsStaticContent, 
                PageNumber, GridxPosition, GridyPosition, GridWidth, GridHeight, BlockSizeID, BlockxPosition, BlockyPosition, BlockWidth, BlockHeight, ColourCMYK, 
                CAST(LPAD(IFNULL(SortOrder,0),4,'0') AS CHAR(200)) as sortpath,
                1 as CategoryLevel,
                (select BlockSizeName from BlockSize b1 WHERE b1.BlockSizeID = Category.BlockSizeID) as BlockSizeName,
                (select count(*) from Category c2 WHERE c2.ParentCategoryID = Category.CategoryID) as CatCount,
                0 as ProdCount
            from Category 
            where IsRoot = 1 and CatalogID = $catalogID
            union all
                select  p.CategoryID,
                        p.CategoryName,
                        p.ParentCategoryID,
                        p.InheritCatAttrID, 
                        p.InheritCatTextID,
                        p.InheritCatImageID,
                        p.InheritCatTemplateID, 
                        (select AttributeSchemaID from Category c3 where c3.CategoryID = p.InheritCatAttrID) as AttSchemaID,
                        (select CategoryTextSchemaID from Category c2 WHERE c2.CategoryID = p.InheritCatTextID) as CatTextSchemaID, 
                        (select CategoryTemplateID from Category c2 WHERE c2.CategoryID = p.InheritCatTemplateID) as TemplateID,   
                        (select TemplateName from CategoryTemplate ct WHERE CategoryTemplateID = TemplateID) as TemplateName, 
                        IFNULL(p.SortOrder,0),
                        p.IsRoot,
                        p.IsCollection,
                        p.IsPanel, 
                        p.IsStaticContent,
                        p.PageNumber,
                        p.GridxPosition,
                        p.GridyPosition,
                        p.GridWidth,
                        p.GridHeight,
                        p.BlockSizeID,
                        p.BlockxPosition, 
                        p.BlockyPosition, 
                        p.BlockWidth, 
                        p.BlockHeight,
                        p.ColourCMYK, 
                        CONCAT(cp.sortpath, ',', LPAD(IFNULL(p.SortOrder,0),4,'0')) as sortpath,
                        cp.CategoryLevel + 1,
                        (select BlockSizeName from BlockSize b1 WHERE b1.BlockSizeID = p.BlockSizeID) as BlockSizeName,
                        (select count(*) from Category c2 where c2.ParentCategoryID = p.CategoryID) as CatCount,
                        (select count(*) from ProductInstance i where i.CategoryID = p.CategoryID) as ProdCount 
                from cat_path AS cp JOIN Category as p on p.ParentCategoryID = cp.CategoryID
            )
            select 
                CategoryID, CategoryName, ParentCategoryID, 
                InheritCatAttrID, InheritCatTextID, InheritCatImageID, InheritCatTemplateID, 
                AttSchemaID, CatTextSchemaID, TemplateID, TemplateName,  
                SortOrder, IsRoot, IsCollection, IsPanel, IsStaticContent, 
                PageNumber, GridxPosition, GridyPosition, GridWidth, GridHeight, BlockSizeID, BlockxPosition, BlockyPosition, BlockWidth, BlockHeight, ColourCMYK, 
                sortpath, CategoryLevel, BlockSizeName, CASE WHEN (IsCollection = 0) THEN CatCount ELSE ProdCount END AS TreeCount from cat_path ORDER BY sortpath 

However, I'd like to be able to show the CategoryName at each level in the hierarchy flattened into a table to make an Excel export much simpler.

What I need is the CategoryName from each level in a ?dynamic column name:

CategoryID | Category1Name | Category2Name | etc

Something like the way you can show a group_concat for a category path, but instead of delimiting with ">" characters within the one field I want them as separate fields.

Upvotes: 0

Views: 35

Answers (0)

Related Questions