
create table [ceshi]
(id int, parentid int, code nvarchar(50))
insert into [ceshi]
select 1,0,'a' union all
select 2,1,'b' union all
select 3,1,'c' union all
select 4,2,'d' union all
select 5,2,'e' union all
select 6,2,'f' union all
select 7,3,'g' union all
select 8,3,'h' union all
select 9,4,'i' union all
select 10,5,'j' union all
select 11,9,'k' union all
select 12,7,'l' union all
select 13,7,'m' union all
select 14,8,'n' union all
select 15,13,'o' union all
select 16,14,'p'
差不多就是這樣的資料
p_id就是上級節點
我有了G的id 應該查出A,C,G,L,M,O
有C的id 應該查出A,C,G,L,M,O,H,N,P.
這應該怎么查詢。
用 WITH AS 么
有上萬條資料的話。
會不會慢。
求教。
uj5u.com熱心網友回復:
create table [ceshi](id int, parentid int, code nvarchar(50),longcode (200))
insert into [ceshi]
select 1,0,'a','1' union all
select 2,1,'b','1.2' union all
select 3,1,'c','1.3' union all
select 4,2,'d','1.2.4' union all
select 5,2,'e','1.2.5' union all
select 6,2,'f','1.2.6' union all
select 7,3,'g','1.3.7' union all
select 8,3,'h','1.3.8' union all
select 9,4,'i','1.2.4.9' union all
select 10,5,'j','1.2.5.10' union all
select 11,9,'k','1.2.4.9.11' union all
select 12,7,'l','1.3.7.12' union all
select 13,7,'m','1.3.7.13' union all
select 14,8,'n','1.3.8.14' union all
select 15,13,'o','1.3.7.13.15' union all
select 16,14,'p','1.3.8.14.15'
加一列長代碼,存他的上下級ID,這樣查是不是更方便。
uj5u.com熱心網友回復:
上萬條資料用CET陳述句不會慢的,加一列路徑查下級倒是方便,查上級好像沒多大用,不如都用CET解決uj5u.com熱心網友回復:
你這種設計且不說符不符合范式的要求,但看這個結果集就沒有實際意義,你在查詢的時候肯定不是要顯示這個ID串,而是要轉化成對應的值,這個轉化的程序也是耗時的,其次,這一條線上的資料只要有一個發生變化,關聯的資訊都需要進行更新,比如中間增加一個節點,對應這條線上的路線都需要更新。
uj5u.com熱心網友回復:
我現在要坐一個關于選單權限相關的功能。
選單就是這樣的結構。
分權限,我覺得..我設計的沒問題。現在就是查詢。。我寫的SQL太慢了。
uj5u.com熱心網友回復:
RETURNS @t_level TABLE ( id UNIQUEIDENTIFIER )AS
BEGIN
DECLARE @aaa TABLE ( sid UNIQUEIDENTIFIER );
;WITH CTET
AS
(
SELECT * FROM dbo.emp_pm_td_classification WHERE exists (
select sid from @aaa s WHERE emp_pm_td_classification.sid =s.sid
)
UNION ALL
SELECT a.* FROM dbo.emp_pm_td_classification AS a INNER JOIN CTET AS b ON b.sid=a.parentid)
insert into @t_level SELECT sid FROM CTET
;with t as
(select sid,parentid from emp_pm_td_classification where
exists (
select sid from @aaa s WHERE emp_pm_td_classification.sid =s.sid
)
union all
select b.sid,b.parentid
from t a
inner join emp_pm_td_classification b on a.parentid=b.sid)
insert into @t_level select sid
from t
@aaa 是查詢出來的 臨時 表, 大概有130條資料。
單執行往@aaa 插資料 , 執行0秒 。
最終查詢結果。4500條左右。執行4秒。
emp_pm_td_classification 總資料,五六千條。
uj5u.com熱心網友回復:
-- 試試這個
/*
create table [ceshi](id int, parentid int, code nvarchar(50))
go
insert into [ceshi]
select 1,0,'a' union all
select 2,1,'b' union all
select 3,1,'c' union all
select 4,2,'d' union all
select 5,2,'e' union all
select 6,2,'f' union all
select 7,3,'g' union all
select 8,3,'h' union all
select 9,4,'i' union all
select 10,5,'j' union all
select 11,9,'k' union all
select 12,7,'l' union all
select 13,7,'m' union all
select 14,8,'n' union all
select 15,13,'o' union all
select 16,14,'p'
*/
go
-- 查找條件
declare @s varchar(100) = 'g'
-- 獲取父節點和子節點的資料
;with ParentCode as(
select id,parentid,code from ceshi where code = @s
union all
select a.id,a.parentid,a.code from ceshi a join ParentCode d on a.id = d.parentid
),ChildCode as (
select id,parentid,code from ceshi where code = @s
union all
select a.id,a.parentid,a.code from ceshi a join ChildCode c on a.parentid = c.id
)
/*
--顯示資料
select * from (
select * from ParentCode
union
select * from ChildCode
) t
*/
--合并字串
select stuff((select ','+ code
from (
select * from ParentCode
union
select * from ChildCode
) t
for xml path('')) , 1,1,'')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/125997.html
標籤:應用實例
