简体   繁体   English

从SQL Server中选择记录,例如下面的数据

[英]Select record from SQL Server like data below

在此处输入图片说明

I have record like below in 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

And I want get result like below using Id in where condition 而且我想在条件中使用Id得到如下结果

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

Thanks, please guide me how can I achieve this? 谢谢,请指导我如何实现这一目标?

Since you want to obtain all the references following the chain of FromRefId you need to use a recursive query, which can be achieved in SQL Server using a recursive common table expression : 由于要获取FromRefId链之后的所有引用, FromRefId需要使用递归查询,这可以在SQL Server中使用递归公用表表达式来实现:

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 SQL小提琴

Note that if instead of searching by Id you search by RefId you can simplify the query a bit: 请注意,如果不是通过搜索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]

You can use the below approach. 您可以使用以下方法。 I have written a Table-valued function, "GetChild". 我已经编写了一个表值函数“ GetChild”。 It iterates through the records recursively to get all dependencies, and finally get the RefId for all those dependencies. 它以递归方式遍历记录以获取所有依赖关系,最后获取所有那些依赖关系的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 SQL小提琴代码

Should be a simple query 应该是一个简单的查询

SELECT * FROM your_table_name WHERE Id = your_desired_id

Why the downgrade? 为什么降级? Isn't that what you were looking for? 那不是您要找的东西吗? I don't think your question is clear. 我认为您的问题不清楚。 Which Id is being referred here?! 在此引用哪个ID?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM