请教个问题 有两个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
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就是你写的那两个。
表链接
是这个意思不?
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