2013-02-15 82 views
1

我有一个像如何连接两个选择在同一个表

Select author_id, count(text) from posts group by author_id 

select author_id, count(text) from posts where postcounter =1 group by author_id 

两个选择语句是有没有办法在一个查询两种说法结合起来?结果长度不同,因此需要在第二个结果集中插入一些0。

非常感谢所有帮助

最好的问候, 西蒙娜

回答

1

您应该使用能够在一个单一的查询来获取这样的:

Select author_id, 
    count(text) TextCount, 
    count(case when postcounter=1 then text end) PostCount 
from posts 
group by author_id 
+0

非常感谢你,它完美的作品! Best,Simone – user299791 2013-02-16 13:50:43

1

这是你在找什么?

select author_id, 
    sum(case when postcounter = 1 then 1 else 0 end) count1, 
    sum(case when postcounter <> 1 then 1 else 0 end) count2, 
    count(text) allcount 
from posts 
group by author_id 
0

你可以尝试联合所有声明?

SELECT `id`,sum(`count`) FROM (
Select author_id as `id`, count(text) as `count` from posts group by author_id 
UNION ALL 
select author_id as `id`, count(text) as `count` from posts where postcounter =1 group by author_id 
    ) 
相关问题