数据库递归查询,求大神帮忙

战斗峰 2015-07-06 12:38:36
表名是 product_category

epc_id int 主键自增列
epc_name varchar(20) 名称
epc_parent_id int

eqc_id epc_name epc_parent_id
----------- -------------------- -------------
1 飞机 0
2 女装 0
3 男装 0
4 鞋子 0
5 箱包 0
6 垂直 1
7 滑跑 1
8 直升机 6
9 滑翔机 7
10 三角翼 7
11 仿真机 7
12 战斗机 7
13 旋翼机 7
14 上装 2
15 下装 2
16 裙子 2
17 袜子 2
18 上装 3
19 下装 3
20 袜子 3
------------------------------------------------
我想查出所有“飞机”后面的子项
如:
飞机-------第 一层
垂直\滑跑 ------- 第二层
直升机\滑翔机 三角翼 战斗机 旋翼机 -----------第三层
....
然后要在myeclipse接收每一层的数据
要如何拼写sql?如何接收数据?
有没有什么好办法?好像要创建一个存储过程?
求大神帮忙!
...全文
212 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
许晨旭 2015-07-06
  • 打赏
  • 举报
回复
只要传一个ID进去就可以使用了
create table #t
(
	eqc_id int,
	epc_name varchar(20),
	epc_parent_id int
)

insert into #t
select  1,'飞机',0 union all
select  2,'女装',0 union all
select  3,'男装',0 union all
select  4,'鞋子',0 union all
select  5,'箱包',0 union all
select  6,'垂直',1 union all
select  7,'滑跑',1 union all
select  8,'直升机',6 union all
select  9,'滑翔机',7 union all
select  10,'三角翼',7 union all
select  11,'仿真机',7 union all
select  12,'战斗机',7 union all
select  13,'旋翼机',7 union all
select  14,'上装',2 union all
select  15,'下装',2 union all
select  16,'裙子',2 union all
select  17,'袜子',2 union all
select  18,'上装',3 union all
select  19,'下装',3 union all
select  20,'袜子',3

;with cte as 
(
	select eqc_id,epc_name,epc_parent_id,1 as leve
	from #t
	where eqc_id=1
	union all
	select  a.eqc_id,a.epc_name,a.epc_parent_id,b.leve+1 as  leve
	from #t a
	join cte b on a.epc_parent_id=b.eqc_id
)
select * from cte

/*
eqc_id      epc_name             epc_parent_id leve
----------- -------------------- ------------- -----------
1           飞机                   0             1
6           垂直                   1             2
7           滑跑                   1             2
9           滑翔机                  7             3
10          三角翼                  7             3
11          仿真机                  7             3
12          战斗机                  7             3
13          旋翼机                  7             3
8           直升机                  6             3

(9 行受影响)
*/
itliyi 2015-07-06
  • 打赏
  • 举报
回复
接收数据直接<select下拉框标签读取那sql返回的数据就行了
itliyi 2015-07-06
  • 打赏
  • 举报
回复

if OBJECT_ID('product_category') is not null
drop table product_category
go
create table product_category(
	eqc_id int,
	epc_name varchar(20),
	epc_parent_id int
)
insert into product_category(eqc_id,epc_name, epc_parent_id)
select 1 ,          '飞机  ',  0  union all
select 2 ,          '女装  ',  0  union all
select 3 ,          '男装  ',  0  union all
select 4 ,          '鞋子  ',  0  union all
select 5 ,          '箱包  ',  0  union all
select 6 ,          '垂直  ',  1  union all
select 7 ,          '滑跑  ',  1  union all
select 8 ,          '直升机',   6 union all
select 9 ,          '滑翔机',   7 union all
select 10,          '三角翼',   7 union all
select 11,          '仿真机',   7 union all
select 12,          '战斗机',   7 union all
select 13,          '旋翼机',   7 union all
select 14,          '上装  ',  2  union all
select 15,          '下装  ',  2  union all
select 16,          '裙子  ',  2  union all
select 17,          '袜子  ',  2  union all
select 18,          '上装  ',  3  union all
select 19,          '下装  ',  3  union all
select 20,          '袜子  ',  3
go
 DECLARE @LevelTable TABLE(
 ID INT, 
 [Level] INT, 
 Sort VARCHAR(50))
DECLARE @Level		INT
SET		@Level=0

INSERT	@LevelTable
SELECT	 eqc_id, @Level, CONVERT(VARCHAR, eqc_id)
FROM	product_category as A
WHERE	A.epc_parent_id= 0

WHILE @@ROWCOUNT>0
BEGIN
    SET		@Level=@Level+1
	
    INSERT	@LevelTable 
    SELECT	A.eqc_id, @Level, B.Sort + CONVERT(VARCHAR, A.eqc_id)
    FROM	product_category A, @LevelTable B
    WHERE	A.epc_parent_id = B.ID AND B.[Level] = @Level - 1 
 
    ORDER BY A.eqc_id
END

--显示结果
SELECT	A.eqc_id, A.epc_parent_id, Replicate('-', B.[Level]*4) + A.epc_name Name, B.Sort
FROM	product_category A, @LevelTable B
WHERE	A.eqc_id=B.ID
ORDER BY B.Sort

战斗峰 2015-07-06
  • 打赏
  • 举报
回复
引用 4 楼 Landa_Ran 的回复:
只要传一个ID进去就可以使用了
create table #t
(
	eqc_id int,
	epc_name varchar(20),
	epc_parent_id int
)

insert into #t
select  1,'飞机',0 union all
select  2,'女装',0 union all
select  3,'男装',0 union all
select  4,'鞋子',0 union all
select  5,'箱包',0 union all
select  6,'垂直',1 union all
select  7,'滑跑',1 union all
select  8,'直升机',6 union all
select  9,'滑翔机',7 union all
select  10,'三角翼',7 union all
select  11,'仿真机',7 union all
select  12,'战斗机',7 union all
select  13,'旋翼机',7 union all
select  14,'上装',2 union all
select  15,'下装',2 union all
select  16,'裙子',2 union all
select  17,'袜子',2 union all
select  18,'上装',3 union all
select  19,'下装',3 union all
select  20,'袜子',3

;with cte as 
(
	select eqc_id,epc_name,epc_parent_id,1 as leve
	from #t
	where eqc_id=1
	union all
	select  a.eqc_id,a.epc_name,a.epc_parent_id,b.leve+1 as  leve
	from #t a
	join cte b on a.epc_parent_id=b.eqc_id
)
select * from cte

/*
eqc_id      epc_name             epc_parent_id leve
----------- -------------------- ------------- -----------
1           飞机                   0             1
6           垂直                   1             2
7           滑跑                   1             2
9           滑翔机                  7             3
10          三角翼                  7             3
11          仿真机                  7             3
12          战斗机                  7             3
13          旋翼机                  7             3
8           直升机                  6             3

(9 行受影响)
*/
就采用这个 感谢大神帮助! 感激不尽啊!
shoppo0505 2015-07-06
  • 打赏
  • 举报
回复
;with data (eqc_id,epc_name, epc_parent_id)as ( select 1 , '飞机 ', 0 union all select 2 , '女装 ', 0 union all select 3 , '男装 ', 0 union all select 4 , '鞋子 ', 0 union all select 5 , '箱包 ', 0 union all select 6 , '垂直 ', 1 union all select 7 , '滑跑 ', 1 union all select 8 , '直升机', 6 union all select 9 , '滑翔机', 7 union all select 10, '三角翼', 7 union all select 11, '仿真机', 7 union all select 12, '战斗机', 7 union all select 13, '旋翼机', 7 union all select 14, '上装 ', 2 union all select 15, '下装 ', 2 union all select 16, '裙子 ', 2 union all select 17, '袜子 ', 2 union all select 18, '上装 ', 3 union all select 19, '下装 ', 3 union all select 20, '袜子 ', 3 ) --select * from data , temp as ( select * from data where eqc_id = 1 union all select data.* from temp inner join data on temp.eqc_id = data.epc_parent_id ) select * from temp order by eqc_id

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧