我有两个选择语句,如
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。
非常感谢您的帮助
祝你好运, 西蒙
答案 0 :(得分: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
答案 1 :(得分:1)
您应该可以使用以下命令在单个查询中获取此内容:
Select author_id,
count(text) TextCount,
count(case when postcounter=1 then text end) PostCount
from posts
group by author_id
答案 2 :(得分:0)
您可以尝试使用union all语句吗?
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
)