我希望我的仪表盘上显示出前五个产品,每个产品上要显示订单总数、浏览量和相对于其他产品所占的百分比,例如:
Game 1 for Xbox (200 orders / 1000 views) 20%
Game 2 for WII (180 orders / 2100 views) 18%
Game 3 for PS3 (170 orders / 390 views) 17%
Game 4 for PS3 (90 orders / 1400 views) 9%
Game 5 for WII (20 orders / 30 views) 2%
针对游戏1的订单数量为200个,相较于总订单数量1000个,占比为20%。也就是说,我的产品中有20%是游戏1。
以下是我的查询:
select
products.name, products.type, products.views, count(*) as orders, ????????
from
products
inner join orders on (products.id = orders.product_id)
group by orders.product_id
如何获得百分比?