包含表间和表内查询的问题

sqlbeginner888 2007-02-04 01:51:09
有两个表

表1(两列)

id A(唯一约束)
1 a
2 b
3 c
4 d
5 e

表2(多列;除V, Condition(bit)列外,其他列都可以有null值-这里我用n来代表null;表中V到Z列中的数据与表1种的id关联)

id V W X Y Z Condition
1 1 n 3 3 5 true
2 3 5 1 2 n False
3 2 2 4 4 n False
4 5 n 1 3 2 True
5 2 n 2 5 4 False

问题如下:
如何找出所有包含c的表2中的记录(第1,2,4三条记录),当Condition列为true时,一并显示字段名Condition,显示效果如下

V:a W:null X:c Y:c Z:e Condition
V:c W:e X:a Y:b Z:null
V:e W:null X:a Y:c Z:b Condition

(谢过各位,感谢帮忙)
...全文
221 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sqlbeginner888 2007-02-05
  • 打赏
  • 举报
回复
谢谢楼上大虾,测试成功,非常感谢
dawugui 2007-02-04
  • 打赏
  • 举报
回复
你的内容是应该已经存在的,不需要输入.只需要使用下面的语句即可.

select
V=(select A from T1 where id=tmp.V),
W=(select A from T1 where id=tmp.W),
X=(select A from T1 where id=tmp.X),
Y=(select A from T1 where id=tmp.Y),
Z=(select A from T1 where id=tmp.Z)
from T2 as tmp
inner join T1 on T1.A='c' --把c改成b就可以了
and (tmp.V=T1.id or tmp.W=T1.id or tmp.X=T1.id or tmp.Y=T1.id or tmp.Z=T1.id)
sqlbeginner888 2007-02-04
  • 打赏
  • 举报
回复
再次表示不解,望楼上大虾见谅,我的表1,一共有十万条记录,请问按你的方法
从insert T1 select 1, 'a' 开始
我将要打
union all select 2, 'b'
union all select 3, 'c'
union all select 4, 'd'
union all select 5, 'e'
一直打到
union all seletc 100,000, 'character 100000',岂非累死我

本人愚钝,还望楼上大虾莫见怪

marco08 2007-02-04
  • 打赏
  • 举报
回复
create table T1(id int, A varchar(10))
insert T1 select 1, 'a'
union all select 2, 'b'
union all select 3, 'c'
union all select 4, 'd'
union all select 5, 'e'

create table T2(id int, V int, W int, X int, Y int, Z int, Condition bit)
insert T2 select 1, 1, null, 3, 3, 5, 1
union all select 2, 3, 5, 1, 2, null, 0
union all select 3, 2, 2, 4, 4, null, 0
union all select 4, 5, null, 1, 3, 2, 1
union all select 5, 2, null, 2, 5, 4, 0


select
V=(select A from T1 where id=tmp.V),
W=(select A from T1 where id=tmp.W),
X=(select A from T1 where id=tmp.X),
Y=(select A from T1 where id=tmp.Y),
Z=(select A from T1 where id=tmp.Z)
from T2 as tmp
inner join T1 on T1.A='c' --把c改成b就可以了
and (tmp.V=T1.id or tmp.W=T1.id or tmp.X=T1.id or tmp.Y=T1.id or tmp.Z=T1.id)

--result
V W X Y Z
---------- ---------- ---------- ---------- ----------
a NULL c c e
c e a b NULL
e NULL a c b

(3 row(s) affected)
sqlbeginner888 2007-02-04
  • 打赏
  • 举报
回复
可能我表达得不好,还望见谅,我的总体意思是当客户给我“c”,我就根据这两个表(我考虑过不要condition了,麻烦),得出

V W X Y Z
---------- ---------- ---------- ---------- --------------------
a NULL c c e
c e a b NULL
e NULL a c b

给我“b”,我同样根据这两个表,得出

V W X Y Z
---------- --------- ----------- ---------- --------------------
c e a b null
b b d d null
e null a c b
b null b e d

感谢楼上大虾,我这两个表都很大,我是初学的,可能不明你的方法,非常感谢你的解答
marco08 2007-02-04
  • 打赏
  • 举报
回复

create table T1(id int, A varchar(10))
insert T1 select 1, 'a'
union all select 2, 'b'
union all select 3, 'c'
union all select 4, 'd'
union all select 5, 'e'

create table T2(id int, V int, W int, X int, Y int, Z int, Condition bit)
insert T2 select 1, 1, null, 3, 3, 5, 1
union all select 2, 3, 5, 1, 2, null, 0
union all select 3, 2, 2, 4, 4, null, 0
union all select 4, 5, null, 1, 3, 2, 1
union all select 5, 2, null, 2, 5, 4, 0


select
V=(select A from T1 where id=tmp.V),
W=(select A from T1 where id=tmp.W),
X=(select A from T1 where id=tmp.X),
Y=(select A from T1 where id=tmp.Y),
Z=(select A from T1 where id=tmp.Z)+(case when tmp.Condition=1 then ' Condition' else '' end)
from T2 as tmp
inner join T1 on T1.A='c' and (tmp.V=T1.id or tmp.W=T1.id or tmp.X=T1.id or tmp.Y=T1.id or tmp.Z=T1.id)

--result
V W X Y Z
---------- ---------- ---------- ---------- --------------------
a NULL c c e Condition
c e a b NULL
e NULL a c b Condition

(3 row(s) affected)
内容概要:本文围绕“基于超局部模型与自抗扰ESO观测器的无模型预测电流控制改进策略”展开研究,提出一种结合超局部模型(ULM)与扩张状态观测器(ESO)的无模型预测电流控制(MFPCC)改进方法,旨在提升永磁同步电机(PMSM)电流环的动态响应性能与抗干扰能力。该策略利用超局部模型对系统行为进行局部逼近,避免依赖精确数学模型,同时引入自抗扰控制中的ESO实时观测并补偿系统内外部扰动,有效抑制参数摄动、负载变化及模型不确定性带来的影响。研究通过Simulink搭建完整的控制系统仿真模型,对传统MFPCC与所提改进策略进行对比分析,验证了新方法在电流跟踪精度、响应速度和鲁棒性方面的优越性。; 适合人群:具备电机控制、现代控制理论及Simulink仿真基础的电气工程、自动化及相关专业的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于高性能电机驱动系统中电流环控制器的设计与优化;②为无模型控制与自抗扰控制的融合应用提供技术参考;③支撑相关课题的仿真验证、论文复现与创新方法研究。; 阅读建议:建议读者结合Simulink仿真模型深入理解控制结构与参数整定过程,重点关注ESO的观测性能与扰动补偿机制,并可通过改变负载条件、参数偏差等工况进行鲁棒性测试,进一步掌握该改进策略的核心优势与适用边界。
内容概要:本文围绕Scratch图形化编程平台,详细阐述了《人体感应灯光系统》这一贴近生活的AI科创作品的设计与教学应用。通过模拟真实智能家居中人体感应灯的工作原理,利用Scratch的侦测、逻辑判断、亮度特效调节等功能,实现了人物靠近自动亮灯、延时熄灭及环境亮度自适应等仿真功能。文章系统拆解了从场景搭建、核心逻辑设计、分层编程实现到调试优化的完整开发流程,并提供了基础版与进阶版可直接导入的源码,支持零基础快速上手与高阶创新拓展。同时构建了“基础—进阶—高阶”三层阶梯式教学体系,适配常规课堂、创客社团与赛事培优等多元教学场景,推动中小学AI教育的生活化、实践化与创新化发展。 适合人群:小学高年级至初中阶段学生,信息技术教师,创客教育从业者,以及参与青少年科创赛事的师生。 使用场景及目标:①作为中小学人工智能通识课程的教学案例,帮助学生理解智能感应与控制逻辑;②用于校内创客社团开展项目式学习;③支撑学生参加AI科创类赛事,完成高质量作品创作与答辩准备;④布置为课后综合实践作业,提升动手能力与科技素养。 阅读建议:建议结合提供的Scratch源码进行实践操作,在复现基础上尝试参数调优与功能扩展,如增加音效提示、多区域感应等,深化对编程逻辑与智能系统设计的理解。

34,876

社区成员

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

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