1
我有一個items
表列計算某列的平均值。的MySQL/PHP:<code>id</code>,<code>name</code>,<code>category</code>和一個<code>ratings</code>表列:<code>id</code>,<code>item</code>,<code>rating</code>如何使用JOIN語句
如何計算每件商品的平均評分?
[id] => item.id
[name] => item.name
[rating] => average rating
這是我的PHP函數至今:
我想有一個PHP數組返回
function getTopRatings($category) {
$pdo = Database::connect();
$sql = "SELECT item, rating, items.id, items.name FROM ratings INNER JOIN items ON ratings.item=items.id WHERE category = '" . $category . "'";
$pdo->query($sql);
foreach ($pdo->query($sql) as $row) {
$ratings[] = array(
"itemid" => $row['id'],
"name" => $row['name'],
"rating" => ?
);
}
Database::disconnect();
}