Sql server 从SQL Server中选择记录,如下面的数据
我在SQL Server中有如下记录Sql server 从SQL Server中选择记录,如下面的数据,sql-server,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 我希望在whe
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得到如下结果
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
谢谢,请指导我如何才能做到这一点 应该是一个简单的查询
SELECT * FROM your_table_name WHERE Id = your_desired_id
为什么降级?那不是你一直在找的吗?我认为你的问题不清楚。这里指的是哪个身份证 由于要获取FromRefId链之后的所有引用,需要使用递归查询,这可以在SQL Server中使用以下方法实现: 请注意,如果不是按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]
您可以使用以下方法。我已经编写了一个表值函数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)
你是说FromRefId吗?这不容易理解,为什么你要从你的查询中得到这个结果!你能解释一下吗?不,我想重新填写RH02 RH03 RH04 RH05 RH06 RH07 RH08 RH09如果你不理解这个问题,你不应该给出答案!我没有投你反对票,但是你应该在回答问题之前,在评论中提问,如果你理解了这个问题。反对票不是他投的,他还没有足够的声誉来反对。我认为你被否决了,因为你答案中的查询没有返回问题中作为示例给出的结果集,我同意这一点。我的答案与问题完全一致。“其中Id=1”。我所说的查询将按Id为您提供值,但我猜这不是他要找的!这在MS-SQL Server中运行良好感谢您的支持。我试过两种建议,一种是你的,另一种是上面提供的。我认为@alberto martinez的建议更合适。谢谢你的支持。我想我会通过你的方法达到我的要求。 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)