I have 4 tables called stores, users, review and rating.
I want to get all the reviews for the corresponding store with detailed information about the user, as well as the overall rating for this store.
I did with almost one request. But the problem is that the store has the same rating several times by the same user, it is considered a single rating. But this rating was correct.
i.e

from this table user_id 3 was rated shop_id 1 as 4 times. Thus, the counter is 4, and total_rating is 17.
My request
select review.comments, users.username, count(distinct rating.id) as rating_count, sum(distinct rating.rating) as total_rating from users left join review on users.id = review.user_id and review.shop_id='1' left join rating on users.id = rating.user_id and rating.shop_id='1' where review.shop_id='1' or rating.shop_id='1' group by users.id, review.user_id, rating.user_id, review.id
When I run this request, I received

But I need total_rating 17 for user_id 3 ..
Tick fiddle
source share