我有一个包含此字段的访问者表:
id|---|browser|---|ip|---|device|---|city|---|state|---|country|---|status
尝试使用morris.js Donut显示访问量最大的设备。它需要百分比值显示为百分比。我试图从MySQL获取数据作为计算值。
SELECT device,
(SELECT COUNT(id) FROM visitor WHERE status = '1') as total,
COUNT(id) as visit FROM visitor WHERE status = '1' GROUP BY device
我想要获得的价值是:
device|---|visit|---|percentage
iOS|---|2|---|40
Android|---|2|---|40
Windows|---|1|---|20
因此,在将特定设备分组之前,查询将计算所有访问者的总数。然后计算每台设备的百分比。
请帮忙
答案 0 :(得分:0)
WOW6432Node
答案 1 :(得分:0)
尝试使用此
select a.device, b.total, c.percent from visitor
left join (select device, count(id) as total from visitor where status='1') as b on b.device=a.device
left join ( select device, b.total/count(id) as percent from visitor where status='1') on c.device=a.device
group by device