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

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

高手能够指点一下如何应用while 对每一条符合记录分别进行查询,谢谢!
...全文
9118 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

34,590

社区成员

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

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