如何实现这样表的选择????

ahone 2007-12-26 09:13:49
表 A
id fid tid
1 12 5
2 22 12
3 24 22
4 26 null


如果输入tid=5

则输出:
id fid tid
1 12 5
2 22 12
3 24 22

就是根据fid和tid之间的关联选择结果,12继承自5,22又继承自12,24又继承自22 所有上面结果
请教sql的写法.
分不够会追加
...全文
132 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
leo_lesley 2007-12-26
  • 打赏
  • 举报
回复
create table A (id  int, fid  int,tid int)
insert A
select 1 , 12 , 5
union select 2 , 22 , 12
union select 3 , 24 , 22
union select 4 , 26 ,null
go
create function f(@val int)
returns varchar(8000)
as
begin
declare @str varchar(1000),@fid varchar(100)

select @str=isnull(@str,'')+','+rtrim(id),@fid=isnull(@fid,'')+rtrim(fid) from A t where tid=@val
while @@rowcount>0
begin
select @str=@str+','+rtrim(id),@fid=rtrim(fid) from A t where tid=@fid
end
set @str=@str+','
return @str
end
go
select * from A where charindex(','+rtrim(id)+',',dbo.f(5))>0

drop function f
drop table A


/* 结果
id fid tid
----------- ----------- -----------
1 12 5
2 22 12
3 24 22

(所影响的行数为 3 行)
*/
leo_lesley 2007-12-26
  • 打赏
  • 举报
回复
create table A (id  int, fid  int,tid int)
insert A
select 1 , 12 , 5
union select 2 , 22 , 12
union select 3 , 24 , 22
union select 4 , 26 ,null


declare @str varchar(1000),@fid varchar(100)
select @str=isnull(@str,'')+','+rtrim(id),@fid=isnull(@fid,'')+rtrim(fid) from A t where tid=5
while @@rowcount>0
begin
select @str=@str+','+rtrim(id),@fid=rtrim(fid) from A t where tid=@fid
end
select * from A where charindex(','+rtrim(id)+',',@str+',')>0

drop table A
ahone 2007-12-26
  • 打赏
  • 举报
回复
就是父子关系 "所有上面结果"---->"所以有上面结果"
昵称被占用了 2007-12-26
  • 打赏
  • 举报
回复

--写个函数
create function fn_Records(
@Tid int
)
returns @R table (Id int,fid int,tid int)
as
begin
insert @r select Id,fid,tid from a where tid=@Tid
while exists (select 1 from a where tid in (select fid from @r) and id not in (select id from @R))
insert @r select Id,fid,tid from a where tid in (select fid from @r) and id not in (select id from @R)
return
end
go

--调用
select * from dbo.fn_Records(5)

areswang 2007-12-26
  • 打赏
  • 举报
回复
你说的继承是个什么关系?
带开环升压转换器和逆变器的太阳能光伏系统 太阳能光伏系统驱动开环升压转换器和SPWM逆变器提供波形稳定、设计简单的交流电的模型 Simulink模型展示了一个完整的基于太阳能光伏的直流到交流电力转换系统,该系统由简单、透明、易于理解的模块构建而成。该系统从配置为提供真实直流输出电压的光伏阵列开始,然后由开环DC-DC升压转换器进行处理。升压转换器将光伏电压提高到适合为单相全桥逆变器供电的稳定直流链路电平。 逆变器使用正弦PWM(SPWM)开关来产生干净的交流输出波形,使该模型成为研究直流-交流转换基本操作的理想选择。该设计避免了闭环和MPPT的复杂性,使用户能够专注于光伏接口、升压转换和逆变器开关的核心概念。 此模型包含的主要功能: •太阳能光伏阵列在标准条件下产生~200V电压 •具有固定占空比操作的开环升压转换器 •直流链路电容器,用于平滑和稳定转换器输出 •单相全桥SPWM逆变器 •交流负载,用于观察实际输出行为 •显示光伏电压、升压输出、直流链路电压、逆变器交流波形和负载电流的组织良好的范围 •完全可编辑的结构,适合分析、实验和扩展 该模型旨在为太阳能直流-交流转换提供一个干净高效的仿真框架。布局简单明了,允许用户快速了解信号流,检查各个阶段,并根据需要修改参数。 系统架构有意保持模块化,因此可以轻松扩展,例如通过添加MPPT、动态负载行为、闭环升压控制或并网逆变器概念。该模型为进一步开发或整合到更大的可再生能源模拟中奠定了坚实的基础。

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧