当前位置:首页 » 编程语言 » sql由子项找父项
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql由子项找父项

发布时间: 2022-10-08 05:41:57

Ⅰ 根据子表如何查询对应的父表

父表需建立主索引,子表只需要建立普通索引,子表 left join 主表

sql同时查询子、父节点的值

直接查就行了,也可以试一下下面的:
select name,value,(select ParentValue from b) as ParentValue from a where OrgID = '001' ;

Ⅲ sql2005 根据子ID递归查询所有父ID,如下,问:dbo怎么才能否去掉

因为你的with后面的名称和表的名称一样了,冲突了。
WITH XI_news_Class 改成with 其他名称

Ⅳ SQL (根据子节点查询父节点信息)

declare @lt table(id int,level int)
declare @level int
declare @findid int
--初始化数据
set @findid = 25/*找nodeId = 25*/
--end of 初始化数据

set @level = 1
insert @lt select @findid,@level
while @@rowcount > 0
begin
set @level = @level + 1
insert @lt select a. parentId,@level
from 你的表名 a, @lt b
where a.nodeId = b.nodeId and b.level = @level - 1
end
--连接得到结果
select a.*
from 你的表名 a inner join @lt b
on a.nodeId=b.nodeId

Ⅳ 如何用sql查找出父子关系关联的结果集

加多一列层级码,会简单很多比如亚洲用‘01’ 中国用‘0101’,北京用‘010101‘,海定用‘01010101’,东城用’01010102‘
这样语句可以
select id as col1,(select name from 表 where code = SubString(a.Code, 1,2)) as col2,
(select name from 表 where code = SubString(a.Code, 3,2)) as col3,
(select name from 表 where code = SubString(a.Code, 5,2)) as col4,
(select name from 表 where code = SubString(a.Code, 7,2)) as col5
from 表 a where Type = 'Distric'
不然
(select d.name from 表 b, 表 c, 表 d where a.parentid = b.id and b.parentid = c.id
and c.parentid = d.id) as col2,...

Ⅵ 求高手帮忙sql写法:树节点放一个表中,怎么用一条语句查询一个节点及对应的所有父节点信息。

建议使用递归,
oracl语法示例如下、
CREATE TABLE TBL_TEST
(
ID NUMBER, --主键
NAME VARCHAR2(100 BYTE),
PID NUMBER DEFAULT 0 --------父节点主键
);
插入测试数据:
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('1','10','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('2','11','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('3','20','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('4','12','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('5','121','2');
从Root往树末梢递归
select * from TBL_TEST
start with id=1
connect by prior id = pid
从末梢往树ROOT递归
select * from TBL_TEST
start with id=5
connect by prior pid = id
SQL server 2005语法示例如下、
CREATE TABLE TBL_TEST
(
ID int,
NAME VARCHAR(100),
PID int DEFAULT 0
);

插入测试数据:

INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('1','10','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('2','11','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('3','20','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('4','12','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('5','121','2');

select * from TBL_TEST

--从Root往树末梢递归

with cte as
(select *,0 as TLevel from TBL_TEST where ID=1
union all
select t1.*,t2.TLevel+1 from TBL_TEST t1 inner join cte t2 on t1.PID=t2.ID)
select * from cte

--从末梢往树ROOT递归

with cte as
(select *,0 as TLevel from TBL_TEST where ID=5
union all
select t1.*,t2.TLevel+1 from TBL_TEST t1 inner join cte t2 on t1.ID=t2.PID)
select * from cte

Ⅶ sql根据子节点查出所有的父节点的

用函数做,根据你的表结构改:
父节点查询子节点
create function GetChildID(@ParentID int)
returns @t table(ID int)
as
begin
insert into @t select id from table where parent_id = @ParentID
while @@rowcount<>0
begin
insert into @t select a.id from table as a
inner join @t as b
on a.parent_id = b.ID
and not exists(select 1 from @t where ID=a.ID)
end
return
end
go

子节点查询父节点
create function GetParentID(@ChildID int)
returns @t table(PID int)
as
begin
insert into @t select parent_id from table where ID=@ChildID
while @@rowcount<>0
begin
insert into @t select a.parent_id from table as a
inner join @t as b
on a.ID=b.PID
and not exists(select 1 from @t where PID=a.parent_id)
end
return
end
go

Ⅷ sql 父类下的子类查询的方法,包含父类的信息

-查询各节点的父路径函数(从父到子)
create function f_pid1(@id varchar(3)) returns varchar(100)
as
begin
declare @re_str as varchar(100)
set @re_str = ''
select @re_str = name from tb where id = @id
while exists (select 1 from tb where id = @id and pid is not null)
begin
select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
end
return @re_str
end
go
--查询各节点的父路径函数(从子到父)
create function f_pid2(@id varchar(3)) returns varchar(100)
as
begin
declare @re_str as varchar(100)
set @re_str = ''
select @re_str = name from tb where id = @id
while exists (select 1 from tb where id = @id and pid is not null)
begin
select @id = b.id , @re_str = @re_str + ',' + b.name from tb a , tb b where a.id = @id and a.pid = b.id
end
return @re_str
end
go

select * ,
dbo.f_pid1(id) [路径(从父到子)] ,
dbo.f_pid2(id) [路径(从子到父)]
from tb order by id

drop function f_pid1 , f_pid2
drop table tb

/*
id pid name 路径(从父到子) 路径(从子到父)

Ⅸ SQL2000 用子节点查最高父节点,并计算相乘数量。

create table tmp
(
PARTNAME varchar(50),
QTY int,
CHILD_ID varchar(50),
PARENT_ID varchar(50)
)
GO
declare @name varchar(50)
set @name='AA'
insert into tmp
select distinct t2.PARTNAME, t1.QTY*t2.QTY,t2.CHILD_ID,t2.PARENT_ID from tree t1,tree t2 where t1.PARENT_ID=t2.CHILD_ID and t1.PARTNAME=@name
while exists(select * from tmp t1,tree t2 where t1.PARENT_ID=t2.CHILD_ID and t2.PARTNAME not in(select PARTNAME from tmp))
begin
insert into tmp
select distinct t2.PARTNAME, t1.QTY*t2.QTY,t2.CHILD_ID,t2.PARENT_ID from tmp t1,tree t2 where t1.PARENT_ID=t2.CHILD_ID and t2.PARTNAME not in(select PARTNAME from tmp)
end;
select PARTNAME,QTY,CHILD_ID from tmp where PARENT_ID=0
drop table tmp