mysql输出拆分测试网页计数,转换次数,百分比

时间:2016-08-19 19:11:35

标签: php mysql

我有一个数据库可以记录各个目标网页的匹配,然后在转换时为每个目标网页添加一个转换。我想输出一个小表格,显示每个目标网页的总点击次数,转化次数和每个页面的转化百分比,如果可能的话,还有日期选择器,例如"日期> = 8月18日"

数据库看起来像这样:

page |visitor ip | conversion | date
pg1  |  x.x.x    |    1       | Aug 19 
pg3  |  x.x.x    |    0       | Aug 19 
pg1  |  x.x.x    |    0       | Aug 19 
pg5  |  x.x.x    |    0       | Aug 19 
pg1  |  x.x.x    |    0       | Aug 19 
pg5  |  x.x.x    |    1       | Aug 19 
pg1  |  x.x.x    |    1       | Aug 19 
pg3  |  x.x.x    |    0       | Aug 19 
pg5  |  x.x.x    |    0       | Aug 19 

想输出一个像这样的表:

page  |  total hits  |   total conversions | conversion rate
pg1   |   4          |     2               |      50%
pg3   |   2          |     0               |       0
pg5   |   3          |     1               |       33%
谢谢!!!!!!!!!!

1 个答案:

答案 0 :(得分:0)

使用此查询:

 SELECT 
    page , 
    count(page) as 'total hits', 
    SUM(IF(conversion=1,1,0)) as 'total conversions',
    CONCAT(ROUND(SUM(IF(conversion=1,1,NULL))/count(page) * 100),'%') as Rate    

 FROM stats GROUP by page ORDER BY COUNT( page ) DESC

要获得此结果:

enter image description here

Live Demo