Hi all!
I have a table for storing submitted ratings of user images.
The rows are :
image_id (unique identifier of the rated image) and rating_num (value from 1-5 depending on the rating). Every time a user clicks to rate an image a row is added to the table (with the image_id and the rating_num).
I use one query to find the image_id which is rated the most :
SELECT image_id, COUNT(image_id) AS countrates FROM ratings GROUP BY image_id ORDER BY countrates DESC LIMIT 1
which works OK.
What I want is to build a query (if possible) to find the image_id with the highest rating.
I have this PHP function to find the overall rating of an image with the given id :
function outOfFive($id) {
$total = 0;
$rows = 0;
$sel = mysql_query("SELECT rating_num FROM ratings WHERE image_id = '$id'");
if(mysql_num_rows($sel) > 0){
while($data = mysql_fetch_assoc($sel)){
$total = $total + $data['rating_num'];
$rows++;
}
$perc = ($total/$rows);
return round($perc,2);
} else {
return '0';
}
} // end function out of 5
but I think it’s not elegant to cycle through this function for every single image_id from the table (there are already over 1,000 rows and counting…). Can someone suggest a good solution please?
Thanks advanced.