我想獲得的收視率,在我的數據庫中的特定項目行填寫。並非所有項目都有1,2,3,4或5的評分;當發生這種情況時,我該如何填寫0
的行?缺失與零
因此,例如,當我爲1,2,3,但不是4和5的收視率,我怎麼能填補這兩個行與0
的?
select
rating,
count(rating) as rating_count,
(count(rating)/(select count(item_id) from ratings where item_id = 3) * 100) as percent,
avg(rating)
from ratings
where item_id = 3
group by rating desc
with rollup
這裏是上面的查詢上述結果,你可以看到有沒有1和2的評價,我怎麼能得到那些rating_count
,percent
和avg(rating)
是零?
@霍根的回答是:
你需要這個只有一個項目或所有項目? –
我需要這一個項目,就像當你查看在谷歌Play商店的應用程序,它的額定誰1,2,3,4,5 –
@GetOffMyLawn的人數 - 無法得到它的工作在mysql彙總似乎與標準的方式做到這一點混亂。 – Hogan