获取GROUP BY中列的平均值

时间:2013-04-28 18:34:24

标签: php mysql sql

我通过对引用者进行分组来获取数据,并且在每个引用者中,我尝试获得每个引用者的平均点击次数。例如:

referer a site
--------------
avg clicks: 12


referer b site
--------------
avg clicks: 4

我使用此代码

SELECT *, COUNT(*)
FROM track
GROUP BY referer
ORDER BY id desc
LIMIT 15    

while ($yazdir=mysql_fetch_array($sqlsorgu))
{
    $count = $yazdir['COUNT(*)'];
    $clicks[] = $yazdir[clicks];
    $score_sum = array_sum($clicks);
    $mean_average = $score_sum / $count;
}

然而,它总是计算错误的数字,也许它也存储以前的数字。获得每个参考者的平均点击次数的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

为什么不

    SELECT *, COUNT(*),AVG(clicks)  
   FROM track GROUP BY referer ORDER BY id desc limit 15