我有一個像如何連接兩個選擇在同一個表
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,
count(text) TextCount,
count(case when postcounter=1 then text end) PostCount
from posts
group by author_id
這是你在找什麼?
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 `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
)
非常感謝你,它完美的作品! Best,Simone – user299791 2013-02-16 13:50:43