I have the following tables in MySQL 8:
Restaurant (Id, Name)Order (Id, RestaurantId)Rating (OrderId, Rating, CreatedAt)
Now I want to calculate rating of a restaurant from past 12 months but also need to get count of all, so it will be shown on front-end like "4.7 stars (120 reviews)", here 4.7 stars are average of 12 months rating, but 120 is count of all ratings. Here's the query which I tried, but count shows 1 with each row:
SELECT COUNT(r.Id), r.Rating FROM `Order` o
JOIN `rating` r ON r.OrderId = o.Id
WHERE o.RestaurantId = 1 AND r.CreatedAt > now() - INTERVAL 12 month
GROUP BY r.Id