2017-10-14 52 views
0

enter image description here從SQL Server中選擇記錄如下面的數據

我在SQL Server中有如下記錄。

Id RefId FromRefId 
    1  RH01 RH00 
    2  RH02 RH01 
    3  RH03 RH01 
    4  RH04 RH03 
    5  RH05 RH02 
    6  RH06 RH03 
    7  RH07 RH04 
    8  RH08 RH02 
    9  RH09 RH05 

而且我希望得到類似結果低於在條件

Where Id=1 
RH02 
RH03 
RH04 
RH05 
RH06 
RH07 
RH08 
RH09 

Where Id=2 
RH05 
RH08 
RH09 

Where Id=3 
RH04 
RH06 
RH07 

Where Id=4 
RH07 

Where Id=5 
RH09 

謝謝,請指引我以ID我怎樣才能做到這一點?

+0

你的意思是FromRefId? – Nikolaus

+0

這並不容易理解,爲什麼你想要查詢這個結果!你可以解釋嗎? – Nikolaus

+0

不,我想REFID RH02 RH03 RH04 RH05 RH06 RH07 RH08 RH09 –

回答

-1

應該是一個簡單的查詢

SELECT * FROM your_table_name WHERE Id = your_desired_id 

爲什麼降級?那不是你要找的東西嗎?我不認爲你的問題很清楚。哪個Id在這裏被引用?!

+0

你不應該給一個答案,如果你不明白的問題!我沒有讓你失望,但你應該在評論中提問,如果你在回答之前理解了這個問題。 – Nikolaus

+0

downvote不是來自他,他沒有足夠的聲望downvote呢。我認爲你被低估了,因爲你的答案中的查詢沒有返回問題中給出的結果集(我同意這個結果集並不清楚)。 –

+0

我完全回答了這個問題。 「哪裏Id = 1」。我說的查詢會給你的價值由Id,但我想這不是他正在尋找! – Ash

0

既然你想獲得所有你需要使用recursive common table expression使用遞歸查詢,可以在SQL Server中實現FromRefId鏈以下引用:

with Recursive_IDs (Id, RefId, FromRefId) as (
    -- anchor query 
    select Id, RefId, FromRefId 
    from IDs 

    union all 

    -- recursive query 
    select IDs.Id, IDs.RefID, Recursive_IDs.FromRefId 
    from IDs 
    inner join Recursive_IDs on Recursive_IDs.RefId=IDs.FromRefId 
) 
select Recursive_IDs.RefId 
from Recursive_IDs 
join IDs on Recursive_IDs.FromRefID=IDs.RefID 
where IDs.id = [the id you want] 

SQL fiddle

注意如果不是通過Id進行搜索,您可以通過搜索RefId來簡化查詢:

with Recursive_IDs (Id, RefId, FromRefId) as (
    -- anchor query 
    select Id, RefId, FromRefId 
    from IDs 

    union all 

    -- recursive query 
    select IDs.Id, IDs.RefID, Recursive_IDs.FromRefId 
    from IDs 
    inner join Recursive_IDs on Recursive_IDs.RefId=IDs.FromRefId 
) 
select Recursive_IDs.RefId 
from Recursive_IDs 
where FromRefId = [the RefId you want] 
+0

**感謝**的支持。我想我會通過你的方法達到我的要求。 –

0

您可以使用下面的方法。我寫了一個表值函數,「GetChild」。它遞歸地遍歷記錄以獲取所有依賴關係,最後獲得所有這些依賴關係的RefId。

 

Create table hierarchy (Id int, RefId varchar(10), FromRefId varchar(10)) 
GO 
insert into hierarchy 
select 1,'RH01','RH00' union all 
select 2,'RH02','RH01' union all 
select 3,'RH03','RH01' union all 
select 4,'RH04','RH03' union all 
select 5,'RH05','RH02' union all 
select 6,'RH06','RH03' union all 
select 7,'RH07','RH04' union all 
select 8,'RH08','RH02' union all 
select 9,'RH09','RH05' 

GO 
-- Table valued Function 
GO 
create function GetChild (@Id INT) 
RETURNS @temp TABLE (RefId varchar(10)) 
AS 
BEGIN 

    declare @tempDependencies table (Id int) 
    insert into @tempDependencies SELECT @Id 

    WHILE ((Select COUNT(Id) from hierarchy where FromRefId in (select RefId from hierarchy where id in (select Id from @tempDependencies)) and id not in (select Id from @tempDependencies)) > 0) 
     BEGIN 
     insert into @tempDependencies 
     Select Id from hierarchy where FromRefId in (select RefId from hierarchy where id in (select Id from @tempDependencies)) and id not in (select Id from @tempDependencies) 
     END 

    insert into @temp 
    Select RefId from hierarchy where FromRefId in (select RefId from hierarchy where id in (SELECT Id from @tempDependencies)) 


    return 
END 
GO 


-- You may call the functions like this: 

select * from GetChild(1) 
select * from GetChild(2) 
select * from GetChild(3) 

SQL Fiddle Code

+0

這在MS-SQL Server中運行良好 –

+0

感謝支持。我已經嘗試了上面提供的另一項建議。我認爲@阿爾貝託 - 馬丁內斯的建議更合適。 –