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

Sort(Tree)列には、次のような出力があります(2行の場合)...

2
1_2

ここで、2はカテゴリTVであり、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 に答える