在sql语句中如何进行循环查询

tw2000 2003-07-06 03:14:29
在一条sql查询语句中,我希望进行这样的查询
foreach (表1中的每一条纪录)
{
select * from 表2 where (表1.a = 表2.a
union

高手能够指点一下如何应用while 对每一条符合记录分别进行查询,谢谢!
...全文
9471 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tw2000 2003-07-15
  • 打赏
  • 举报
回复
真是太棒了,前段时间没办法只好在程序里进行统计,慢啊,无法忍受,这是解决问题了,谢谢:zjcxc(邹建) 以及所有热心人。谢谢
cxz7531 2003-07-09
  • 打赏
  • 举报
回复
学习
zjcxc 元老 2003-07-09
  • 打赏
  • 举报
回复
--想了很久,终于找到了解决办法,你试试这过

/*
部门人员统计-涉及到树形目录统计
*/
begin tran
--创建数据测试环境
create table #bm(部门ID int,部门名称 varchar(10),上级部门ID int)
create table #ry(人员ID int identity(1,1),所属部门ID int)

insert into #bm
select 1,'A部门',0
union all select 10,'A-1部门',1
union all select 11,'A-2部门',1
union all select 12,'A-2部门',1
union all select 101,'A-1-1部门',10
union all select 102,'A-1-2部门',10
union all select 103,'A-1-3部门',10
union all select 111,'A-2-1部门',11
union all select 112,'A-2-2部门',11
union all select 113,'A-2-3部门',11
union all select 121,'A-3-1部门',12
union all select 122,'A-3-2部门',12
union all select 123,'A-3-3部门',12

insert into #ry
select 1
union all select 1
union all select 10
union all select 10
union all select 10
union all select 11
union all select 11
union all select 12
union all select 101
union all select 102
union all select 102
union all select 103
union all select 111
union all select 111
union all select 121
union all select 121
union all select 122
union all select 123
union all select 123

--生成指定部门及其所有下属部门的临时表,这里是:部门id=1,如果统计其他部门,更改:部门id=你要统计的部门id
select cast(部门id as varchar(1000)) as 部门编号累计, 部门id,部门名称 into #bmtemp
from #bm where 部门id=1

while exists(select 1 from #bm
where 上级部门id in (select 部门id from #bmtemp)
and 部门id not in(select 部门id from #bmtemp))
insert into #bmtemp
select (select 部门编号累计 from #bmtemp where 部门id=a.上级部门id)
+'-'+cast(部门id as varchar)
,部门id,部门名称 from #bm a
where 上级部门id in (select 部门id from #bmtemp)
and 部门id not in(select 部门id from #bmtemp)


--得到人数统计的结果
select 部门id,部门名称,count(人员id) as 本部门人员合计
,(select count(人员id) from #ry where 所属部门ID in (select 部门id from #bmtemp where 部门编号累计 like a.部门编号累计+'%' )) as 本部门人员总计
from #bmtemp a,#ry b
where a.部门id=b.所属部门ID
group by a.部门id,部门名称,a.部门编号累计
order by a.部门id

rollback tran
tw2000 2003-07-08
  • 打赏
  • 举报
回复
上述的解答并没有给出答案,

---把所有子部门ID插入@temp 的语句仅仅是将某一部门的子部门全部放进了临时表中,

select a.部门ID,count(b.人员ID ) from 部门 a join 人员 b on a.部门ID=b.所属部门ID
where a.部门ID in (select id from @temp) group by a.部门ID
上述语句则对每部门的人数分别进行总计,而并没有将子部门的人数进行合计,咳,我的语文不行了,举个例子(数据中存在的数据)
部门名称 人数
部门1, 5
子部门10,11,12 各6人
第三级部门101,102,103 各7人
111,112,113,
121,122,123,(每个子部门有三个三级部门)
进行这样的统计 部门1关心下级部门的人数,结果应该是
部门1, 5
子部门 10, 21+6=27
11, 27
12 27
哥们如何去做啊
I_wanttoknow 2003-07-08
  • 打赏
  • 举报
回复
用游标解决循环
游标可以嵌套使用,肯定可以满足要求,不过效率...
zjcxc 元老 2003-07-08
  • 打赏
  • 举报
回复
很难统计,用游标吧
qiubolecn 2003-07-08
  • 打赏
  • 举报
回复
一个好的游标,绝对比那些差的查询语句好。
lggege8000 2003-07-06
  • 打赏
  • 举报
回复
你好象你没有将条件说清楚,不过你已经解决问题了,你也可以解贴了。
zjcxc 元老 2003-07-06
  • 打赏
  • 举报
回复
你的另一个贴子不是已经给出答案了吗?

declare @temp table (id char(8))

insert @temp values ('上级部门的ID值')

while exists (
select id from 部门
where 上级部门ID in (select id from @temp)
and 部门ID not in (select id from @temp)
)
insert @temp
select id from 部门
where 上级部门ID in (select id from @temp)
and 部门ID not in (select id from @temp)
---把所有子部门ID插入@temp


select a.部门ID,count(b.人员ID )
from 部门 a join 人员 b on a.部门ID=b.所属部门ID
where a.部门ID in (select id from @temp)
group by a.部门ID
tw2000 2003-07-06
  • 打赏
  • 举报
回复
我觉得关键在于对于每一个二级部门需要用一个循环语句,这样可以将所有的子部门的人员进行统计。
tw2000 2003-07-06
  • 打赏
  • 举报
回复
不是这样的,请看这条贴子
http://expert.csdn.net/Expert/topic/1995/1995029.xml?temp=.605694
两张表
部门(部门ID,部门名称,上级部门ID,...)
人员 (人员ID ,所属部门ID,...)
假设有三层部门,如何设计一个SQL语句,
使得能够查询到全部部门的人员统计清单,(包含本级直属的以及下级部门所属的人员),如何做?具体显示就是
部门名称 人数
全体 XX
二级部门1 XX
二级部门2 XX
。。。

我的想法是需要统计整个部门的人员,于是
首先 将二级部门放进临时表中,也就是表1
然后
foreach (表1中的每一条纪录)
{
select 部门.id count(人员.id) from 表2 outer join (表1.a = 表2.a
union



zjcxc 元老 2003-07-06
  • 打赏
  • 举报
回复
你是否想查出 表2 中所有 a字段 与表1 a字段 相同的记录?

如果是这样的话,可以以直接用:

select * from 表2 where a in(select 1 from 表1)



select 表2.* from 表2 left join 表1 on 表2.a=表1.a
lggege8000 2003-07-06
  • 打赏
  • 举报
回复
你是相查表中所有条件为表1.a=表2.a 的记录吗
如果是 可用以下语句
select * from 表2 where 表2.a in (select a from 表1)
mjhnet 2003-07-06
  • 打赏
  • 举报
回复
select * from 表2 left outer join 表1 on 表1.a = 表2.a
内容概要:本文针对无刷直流电机驱动的电子机械制动(EMB)执行器,建立了考虑Stribeck摩擦特性的非线性耦合动力学模型,并在Simulink环境完成了系统级仿真分析。研究综合集成了电机动力学、齿轮传动机构与制动执行机构的动力学特性,构建了高保真的机电一体化系统模型。重点引入Stribeck摩擦模型以精确描述低速工况下执行器内部存在的静摩擦、粘滞摩擦与库仑摩擦之间的过渡行为,有效提升了系统在启停、反向运动等瞬态过程的动态响应仿真精度。通过多工况仿真验证了模型的有效性,能够准确反映摩擦引起的爬行、滞后与定位误差等非线性现象,为EMB系统的高性能控制算法设计(如摩擦补偿、滑模控制)与结构优化提供了高可信度的仿真平台。; 适合人群:从事汽车电子制动系统、电机驱动控制、机电系统建模与仿真研究的研究生、科研人员及工程技术人员,需具备扎实的机械动力学、自动控制理论基础和MATLAB/Simulink仿真能力。; 使用场景及目标:①用于高精度电子机械制动系统的设计验证与性能预测;②为消除摩擦非线性影响的先进控制策略(如自适应控制、智能控制)提供精确的被控对象模型;③深入探究Stribeck摩擦等非线性因素对系统动态性能(如响应延迟、稳态误差)的作用机理; 阅读建议:读者应结合提供的Simulink模型文件,深入剖析Stribeck摩擦模块的数学实现与参数辨识方法,建议通过改变输入指令(如阶跃、正弦)和负载条件进行对比仿真,以直观理解非线性摩擦对系统动态特性的影响。

34,875

社区成员

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

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