我有下表
id item_id rating
1 10 1
2 10 2
3 10 2
4 10 3
5 10 3
6 10 3
7 10 4
如何编写查询以便获得如下结果:
$ratings = array(
1 => 1,
2 => 2,
3 => 3,
4 => 1,
5 => 0
);
我需要使用这个查询来编写一个 php 函数来计算这个函数的平均评分:
$total = 0;
$count = 0;
foreach($ratings as $number=>$frequency) {
$total += $number * $frequency;
$count += $frequency;
}
return $total / $count;