algorithm used to calculate 5 star ratings

前端 未结 14 1155
挽巷
挽巷 2021-01-29 17:49

I need to calculate 5-star ratings like the one on Amazon website. I have done enough search to find what is the best algorithm, but I am not able to get a proper answer. For ex

14条回答
  •  误落风尘
    2021-01-29 18:48

    Super helpful reply by Blindy, here's the PHP code that's based on it. Some may find useful. The results will be 4.11 as per OP's example:

    $ratings = array(
    5 => 252,
    4 => 124,
    3 => 40,
    2 => 29,
    1 => 33
    );
    
    function calcAverageRating($ratings) {
    
    $totalWeight = 0;
    $totalReviews = 0;
    
    foreach ($ratings as $weight => $numberofReviews) {
        $WeightMultipliedByNumber = $weight * $numberofReviews;
        $totalWeight += $WeightMultipliedByNumber;
        $totalReviews += $numberofReviews;
    }
    
    //divide the total weight by total number of reviews
    $averageRating = $totalWeight / $totalReviews;
    
    return $averageRating;
    }
    

    How to build the above $ratings array

    Example pseudo code, but which should work that explains how to build the $ratings array when info is stored in DB assuming you have a table called "ratings" and a column called "rating". In this case it's 1 join, you would need to do 4 joins to get all ratings, but this should get you started:

    SELECT count(c1.rating) as one_star, count(c2.rating) as two_star  
    FROM ratings c1 
    LEFT OUTER JOIN
    ratings c2
    ON
    c1.id = c2.id
    WHERE
    c1.rating = 1
    AND
    c2.rating = 2
    

    another approach suggested in comments

    SELECT SUM(rating = 1) AS one_s ,SUM(rating = 2) AS two_s ,SUM(rating = 3) as three_s FROM reviews where product_id = 9
    

提交回复
热议问题