0

在此处输入图像描述我有以下 CTE 查询

;WITH cte AS (
    SELECT 0 AS lvl, id, catName, parent,
        CAST(id AS VARCHAR(128)) AS Sort
    FROM CategoriesMap WHERE id =2
    UNION ALL
    SELECT p.lvl + 1, c.id, c.catName, c.parent,
        CAST(CAST(c.id AS VARCHAR) + '_' + p.Sort   AS VARCHAR(128))
    FROM CategoriesMap c
    INNER JOIN cte p ON p.parent = c.id
)
select * from cte

排序(树)列有这样的输出(两行)......

2
1_2

哪里,2是电视类别,1_2表示这是树状图(1: Internet Tv, 2 = Jadoo Tv)

现在我也可以返回带有类别代码的类别名称吗?

就像是

2:Jadoo Tc

1_Internet Tv: 2_Jadoo Tv

谢谢

4

2 回答 2

2
;WITH cte AS (
    SELECT 0 AS lvl, id, catName, parent,
        CAST(CAST(id AS VARCHAR(128)) + ':' + catName AS VARCHAR(128)) AS Sort
    FROM CategoriesMap WHERE id =2
    UNION ALL
    SELECT p.lvl + 1, c.id, c.catName, c.parent,
        CAST(CAST(c.id AS VARCHAR) + ':' + c.catName + '_' + p.Sort AS VARCHAR(128))
    FROM CategoriesMap c
    INNER JOIN cte p ON p.parent = c.id
)
select * from cte
于 2012-09-14T07:04:59.410 回答
0
;WITH cte AS (
    SELECT 0 AS lvl, id, catName, parent,
        CAST(id AS VARCHAR(128)) AS Sort
    FROM CategoriesMap WHERE id =2
    UNION ALL
    SELECT p.lvl + 1, c.id, c.catName, c.parent,
        CAST(CAST(c.id AS VARCHAR) + '_'+c.catName+':' + p.Sort   AS VARCHAR(128))
    FROM CategoriesMap c
    INNER JOIN cte p ON p.parent = c.id
)
select * from cte
于 2012-09-14T11:14:57.607 回答