2017-10-14 87 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

感谢支持。我已经尝试了上面提供的另一项建议。我认为@阿尔贝托 - 马丁内斯的建议更合适。 –

相关问题