首页 新闻 会员 周边

sql问题

0
[已解决问题] 解决于 2010-08-10 16:25

一个表中有多条数据,要通过一条数据的id查找这条数据的下级数据(下级数据中包含上级数据的id号,下级数据可能包含的还有下级数据) 怎么通过sql语句查找数据

迷.的主页 迷. | 初学一级 | 园豆:4
提问于:2010-08-09 11:45
< >
分享
最佳答案
0

--查询表dealers中dealerID为100的所有子dealer.
Declare @DealerID int
set @DealerID = 100;
with ChildrenDealer(DealerID,DealerName) as
(
select DEALERID, DEALERName from dealers where DEALERID=@DealerID
union All
select a.DEALERID, DEALERName from dealers a inner join ChildrenDealer b on a.PARENTID = b.DealerID

)
select * from ChildrenDealer  

cct | 菜鸟二级 |园豆:265 | 2010-08-09 13:58
其他回答(8)
0

一条是查不出来的,就这就是无限级分类吧?

客户端遍历查询。

Astar | 园豆:40805 (高人七级) | 2010-08-09 11:54
0

在SQL里做很复杂,建议还是在程序里根据父类查询子类递归绑定。

kyo-yo | 园豆:5587 (大侠五级) | 2010-08-09 12:02
0

sql server 递归查询,比Oracle的要复杂点.

http://database.e800.com.cn/articles/2008/418/1208459379686727322_1.html

Launcher | 园豆:45045 (高人七级) | 2010-08-09 12:33
0

SQL SERVER 可以用with

ORACLE可以用connect by

cnhzlt | 园豆:399 (菜鸟二级) | 2010-08-09 12:36
0
邀月 | 园豆:25475 (高人七级) | 2010-08-09 13:27
0

select * from table where id in (select id  from table where parentid=1 ) or parentid in (select id from table where parentid=1)

 table: 是你要查询的表,parentid:是下级数据中包含的上级数据的id号

Michelle 米雪儿 | 园豆:209 (菜鸟二级) | 2010-08-09 13:27
0

CTE 递归是可以解决的 楼上已经有demo了

码尔代夫iimax | 园豆:3138 (老鸟四级) | 2010-08-09 14:22
0

用表值函数,做递归

递归无限级部门用过

xmao-xmao | 园豆:69 (初学一级) | 2010-08-09 15:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册