Home > Database > Mysql Tutorial > SQLSERVER2005 中树形数据的递归查询

SQLSERVER2005 中树形数据的递归查询

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 18:06:30
Original
1214 people have browsed it

在园子里看到一个文章,涉及到树形数据的递归查询,觉得有更好的方法,就随手写一个,以备用之

问题描述。借用了adinet的问题。参见:

今天做项目遇到一个问题,

有产品分类A,B,C顶级分类,

期中A下面有a1,a2,a3子分类.

但是a1可能共同属于A和B,然后我的数据库是这样设计的

如果想要查询A的所有子类的话就要查询parent中包含1的,所以就萌生了这个办法.呵呵,
解决方案
代码如下:
with cte as
(select * from tb where id='1'
union all
select tb.* from tb join cte on charindex(cast(cte.id as varchar(10)),tb.parentid)>0)
select * from cte

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template