我想计算评级系统的平均值,但我在 innodb 中有普通表,在 json 中有一个列,名称为:“评论”,结构如下:
{"comments":
[
{"name": "Jonh", "text": "nice phone", "star": 4},
{"name": "igon", "text": "not good", "star": 2},
{"name": "marco", "text": "i not like this", "star": 3},
{"name": "david", "text": "good product", "stelle": 5}
]
}
Run Code Online (Sandbox Code Playgroud)
现在我需要计算平均星级。它是在 sql 中还是在 php 中?在 sql 中我不知道如何,在 php 中我有查询只提取全明星的问题,例如:
$reviews_nn = $rowprod["reviews"];
$reviews = json_decode($reviews_nn,true);
$max = 0;
$n = 0;
foreach ($reviews[comments][star] as $rate => $count) {
echo 'Seen ', $count, ' ratings of ', $rate, "\n";
$max += $rate …Run Code Online (Sandbox Code Playgroud)