数据库的结构是
id patentID
1 0
2 1
3 2
4 3
5 4
6 5
7 6
8 0
9 8
10 0
11 8
12 9
……
当只知道id为某一值或某几值时,怎么能够查出给出id的所有父级和他们的子级
如:id=(3,4)时
怎么写能查出
id patentID
1 0
2 1
3 2
4 3
5 4
6 5
7 6
id patentID
1 0
2 1
3 2
4 3
5 4
6 5
7 6
8 0
9 8
10 0
11 8
12 9
……
当只知道id为某一值或某几值时,怎么能够查出给出id的所有父级和他们的子级
如:id=(3,4)时
怎么写能查出
id patentID
1 0
2 1
3 2
4 3
5 4
6 5
7 6
解决方案 »
- SQL判断返回结果
- SQLServer中,在一个已经有大量记录(如:1000W条)的表里添加一条记录,速度会不会慢于在一个空表里添加一条记录
- 多表联合查询问题, 高手帮看下。
- 在sql server 2000标准版中,有sql语句自动生成的功能吗?
- 如何在把文本数据导入数据库时,去掉重复的记录?
- 数据库复制的SQL 命令
- 一个存储过程怎么得到另一个存储过程返回的记录集啊?
- 关于权限的问题用SQL语句实现?急急!!!
- 请推荐几本用VC++开发数据库应用程序的好书。
- 当查询条件为:检索出one为空的所有记录,sql语句如何写?
- 请大家帮我看一下我遇到的问题,很急,分不是问题
- 请教一个统计的SQL语句.
insert test_dg(id,parent_id) select
1, 0 union all select
2, 1 union all select
3, 2 union all select
4, 3 union all select
5, 4 union all select
6, 5 union all select
7, 6select * from test_dgcreate procedure usp_getchildandparent(@c_id int,@p_id int)
AS
begin
declare @t_id integer
declare @tc_id integer
declare @tp_id integer
create table #t(id int,parent_id int)
if @c_id<@p_id
BEGIN
set @t_id=@c_id
set @c_id=@p_id
set @p_id=@t_id
END
set @tc_id=@c_id
set @tp_id=@p_id
set @tc_id=@tc_id-1
set @tp_id=@tp_id-1
while exists(select * from test_dg where id=@tc_id and parent_id=@tp_id)
BEGIN
insert into #t(id,parent_id) select id,parent_id from test_dg where id=@tc_id and parent_id=@tp_id
set @tc_id=@tc_id-1
set @tp_id=@tp_id-1
end
set @tc_id=@c_id
set @tp_id=@p_id
set @tc_id=@tc_id+1
set @tp_id=@tp_id+1
while exists(select * from test_dg where id=@tc_id and parent_id=@tp_id)
BEGIN
insert into #t(id,parent_id) select id,parent_id from test_dg where id=@tc_id and parent_id=@tp_id
set @tc_id=@tc_id+1
set @tp_id=@tp_id+1
end
select * from #t order by id
drop table #t
endexec usp_getchildandparent 4,3