Skip to content

Commit

Permalink
modify SQL query in Product model to calculate rating percentage dist…
Browse files Browse the repository at this point in the history
…ribution
  • Loading branch information
Divyeshhhh committed Jun 9, 2024
1 parent d0ae2e1 commit 018f243
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,18 @@ public function getRatingDistribution(): array
{
// Query the database to get the percentage distribution of ratings
$query = <<< EOL
SELECT rating,
COUNT(*) * 100.0 / (
SELECT COUNT(*)
SELECT rating,
COUNT(*) * 10.0 / (
SELECT COUNT(*)
FROM order_product op
JOIN `order` o ON op.order_id = o.order_id
WHERE op.product_id = :product_id
) AS percentage
FROM review r
JOIN order_product op ON r.client_id = op.client_id
JOIN `order` o ON op.order_id = o.order_id
WHERE r.product_id = :product_id
AND op.product_id = :product_id
) AS percentage
FROM review
WHERE product_id = :product_id
GROUP BY rating
JOIN `order` o ON r.client_id = o.client_id
JOIN order_product op ON op.order_id = o.order_id
WHERE op.product_id = :product_id
GROUP BY rating;
EOL;

$params = ['product_id' => $this->product_id];
Expand Down

0 comments on commit 018f243

Please sign in to comment.