1. 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
2. sql 知道父節點,查詢所有的子節點,運用游標,遞歸,存儲過程
呃,因為我不清楚你的表裡,這個BPROD
char(15)
老爸
裡面記錄的值是不是和BMWHS
對應的,所以只能說個大概。
select
sys_connect_by_path(username,'>')
"Path"
from
tmbm
start
with
id=1
connect
by
prior
id=parentid;
其中,id是你要遍歷的起始點,比如你想從
廠號=XX
的這個父節點開始尋找他所有的子節點,這里就換成
start
with
BMWHS=XX
然後,connect
by
prior
id=parentid,這里id=parentid,簡單解釋就是尋找其他記錄里,parentid和我的id相同的記錄,也就是找子節點。
應該是換成你的BMWHS=BPROD(我不知道你的BPROD
BCHLD
和哪個屬性是對應的,是BMWHS嗎?)
如果是的話就是下面這樣(username是你要返回的值,假設你還是要返回
BSEQ
序號)
select
sys_connect_by_path(BSEQ,'>')
"Path"
from
tmbm
start
with
BMWHS=XX
connect
by
prior
BMWHS=BPROD;
3. 求高手幫忙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
4. SQL語句查詢出父節點下的所有子節點
createtable##tmp_users(idint,usernamenvarchar(255),parentidint)
declare@IDint
select@ID=idfromt_Userstwhereexists
(select*fromt_Userst2wheret2.id=t.parentidandt2.username='user1')
execAddSons@ID
select*from##tmp_users
droptable##tmp_users
--存儲
createprocereAddSons@idint
as
ifexists(select*fromt_Userswhereparentid=@id)
begin
declare@tmp_IDint
declarecurcursorfor
selectidfromt_Userswhereparentid=@id
opencur
fetchnextfromcurinto@tmp_ID
while@@FETCH_STATUS=0
begin
insertinto##tmp_users
select*fromt_Userstwhereid=@tmp_ID
ifexists(select*fromt_Userswhereparentid=@tmp_ID)
begin
execAddSons@tmp_ID
end
fetchnextfromcurinto@tmp_ID
end
closecur
DEALLOCATEcur
end
--遞歸調用,不知道是否想要這樣
5. SQL通過父節點獲取所有子節點
這個很簡單啊
表結構一般如下tablename(表名)
id--節點ID, name-- 節點名稱 parentid父節點ID,
-----獲取節點號為6下的所有子節點
select * from tablename t start with id =6 connect by prior id=parentid