求一组复杂SQL语句。

billy_zh billy_zh 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
现在一类别表,字段如下:
class_id int, // 类别ID
parent_id int, // 父类别ID
[level] int, // 类别层次,取1-4,表示1到4级
name varchar(50) // 类别名称

现求一组SQL语句能实现以下输出:

示例数据:
class_id parent_id [level] name
1 0 1 test1
2 1 2 test2
3 1 2 test3
4 2 3 test4

输出:
class_id c1_id c1_name c2_id c2_name c3_id c3_name c4_id c4_name
1 1 test1 null null null null null null
2 1 test1 2 test2 null null null null
3 1 test1 3 test3 null null null null
4 1 test1 2 test2 4 test4 null null
其中c1_id, c1_name 为1级类别的ID和名称,
c2_id, c2_name 为2级类别的ID和名称,
c3_id, c3_name 为3级类别的ID和名称,
c4_id, c4_name 为4级类别的ID和名称.
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
billy_zh
MS-SQL Server 34,876 成员 254,635 内容