如何评估该类别及其子类别? [英] how to sperate the category and its sub category?

查看:92
本文介绍了如何评估该类别及其子类别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的桌子结构..





id name parentid

1 Mobile null

2诺基亚1

3诺​​基亚lumia 2

4诺基亚Lumia520 3

5计算机null

6 dell 5

7 lenovo 5

8 dell 697 6

9 lenovo 220 7



现在如果我选择手机我想输出像



手机

诺基亚

诺基亚Lumia

诺基亚Lumia520



有可能吗?

this is my table structure..


id name parentid
1 Mobile null
2 Nokia 1
3 nokia lumia 2
4 nokia Lumia520 3
5 computer null
6 dell 5
7 lenovo 5
8 dell 697 6
9 lenovo 220 7

now if i select mobile I want output like

mobile
nokia
nokia Lumia
nokia Lumia520

is it possible?

推荐答案

是的,有可能的。您需要使用公用表格式(CTE) [ ^ ] 这里。有关详细信息,请参阅MS Technet文章 - 使用公用表表达式的递归查询

[ ^ ]
Yes, it is possible. You will need to use Common Table Expressions (CTE)[^] here. For more details see the MS Technet article - Recursive Queries Using Common Table Expressions
[^]


使用它:



use this:

with cte(parent_id,id)
as
(
select parent_id,Id from table where parent_id is null
union all

select parent_id,ID from table as a inner join cte as b on b.id=a.parent_id
)
select *from cte


这篇关于如何评估该类别及其子类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆