首页 新闻 会员 周边

关于SQL的问题

0
悬赏园豆:200 [已解决问题] 解决于 2014-04-21 15:17

请教个问题 有两个sql语句
--------------------------------

select tname,count(1) count1 from
(
select * from table1 t1 where t1.sid=0
)
group by tname
---------------------------------------

select tname,count(1) count2 from
(
select * from table1 t1 where t1.sid>0
)
group by tname

-----------------------------------

这两条记录 分别为:

tname    count1
 XXXX       2
 YYYY       5

-----------------
tname    count2
XXXX       3
ZZZZ       7

我希望通过一条sql语句查询最得到的最后结果是

sectionname     count1      count2
XXXX                 2               3
YYYY                  5               0
ZZZZ                 0                7

sql
guanzhong的主页 guanzhong | 初学一级 | 园豆:5
提问于:2014-04-21 14:39
< >
分享
最佳答案
0

Select Tname, sum(count1) as count1, sum(count2) as count2 from

(

Select Tname, count1, 0 as count2 from 查询1

UNION ALL

Select Tname, 0 as count1, count2 from 查询2

)

group by tname

这样算一句吧?

查询1、查询2就是你写的那两个。

收获园豆:190
爱编程的大叔 | 高人七级 |园豆:30839 | 2014-04-21 14:54
其他回答(2)
0

表链接

收获园豆:10
FortuneGril | 园豆:300 (菜鸟二级) | 2014-04-21 14:58
0

是这个意思不?

select tname,

sum(

case when sid = 0 then 1 else 0 end

) count1,

sum(

case when sid > 0 then 1 else 0 end

) count2,

 from
group by tname

醉低调 | 园豆:128 (初学一级) | 2014-04-21 15:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册