如何把几条SQL语句返回的结果集合成一个结果集输出.(100分)

  • 主题发起人 主题发起人 xiaolv
  • 开始时间 开始时间
X

xiaolv

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好!
在最的工作过程中,遇到一个问题,不知如何解决,请大家帮忙.万分感谢.
数据库为SQL SERVER2000.

select lk,count(*) as a from dzjc group by lk
返回lk及已有数量.
select lk,count(*) as b from dzjcimg where zxjg=1 group by lk
返回lk及已及执行结果为1的结果
select lk,count(*) as c from dzjcimg where zxjg=2 group by lk
返回lk及执行结果为2的结果

如何用一条SQL语句 或 视图 或 存存储过程 返回以下型式:
---------------------------------------
lk | a | b | c |
------------------------------
即连成一个表(结果集)返回.
 
select lk,count(*) as a from dzjc group by lk
union
select lk,count(*) as b from dzjcimg where zxjg=1 group by lk
union
select lk,count(*) as c from dzjcimg where zxjg=2 group by lk

 
select a.lk ,a.a,b.b,c.c from (select lk,count(*) as a from dzjc group by lk) a left join
(select lk,count(*) as b from dzjcimg where zxjg=1 group by lk) b on a.lk=b.lk left join
(select lk,count(*) as c from dzjcimg where zxjg=2 group by lk) c on a.lk=c.lk

 
多谢w12345678兄的答案,非常正确。
onlyonekgx兄,你所写的SQL语句不能实现我所提出的功能。因此,不能给你加分,很报
前歉.
 
后退
顶部