--查询表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
一条是查不出来的,就这就是无限级分类吧?
客户端遍历查询。
在SQL里做很复杂,建议还是在程序里根据父类查询子类递归绑定。
sql server 递归查询,比Oracle的要复杂点.
http://database.e800.com.cn/articles/2008/418/1208459379686727322_1.html
SQL SERVER 可以用with
ORACLE可以用connect by
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号
CTE 递归是可以解决的 楼上已经有demo了
用表值函数,做递归
递归无限级部门用过