我有两个选择语句,例如
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。
非常感谢您的帮助
最好的问候,西蒙娜
我有两个选择语句,例如
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。
非常感谢您的帮助
最好的问候,西蒙娜
这是你要找的吗?
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
您应该能够在单个查询中使用:
Select author_id,
count(text) TextCount,
count(case when postcounter=1 then text end) PostCount
from posts
group by author_id
您可以尝试使用 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
)