简体   繁体   English

SUM()在MySQL中不起作用:SUM()与DISTINCT

[英]SUM() not working in MySQL : SUM() with DISTINCT

I have 4 tables called shops, users, review and rating. 我有4个表,称为商店,用户,评论和评级。

I want to get all reviews for the corresponding shop with reviewed user details and also overall rating for that shop. 我想获得相应商店的所有评论,包括评论的用户详细信息以及该商店的整体评分。

I have done almost with the single query. 我几乎完成了单个查询。 But the problem is if the shop has same rating for multiple times by same user its consider as single rating. 但问题是,如果商店对同一用户进行多次相同评级,则将其视为单一评级。 But that rating count was correct. 但该评级数是正确的。

ie

在此输入图像描述

from this table user_id 3 was rated shop_id 1 as 4 times. 从该表中,user_id 3被评为shop_id 1为4次。 So the count is 4 and total_rating is 17. 因此计数为4,total_rating为17。

My query is 我的疑问是

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 query I got 当我运行此查询时,我得到了

在此输入图像描述

But I need total_rating 17 for user_id 3.. 但我需要total_rating 17 for user_id 3 ..

Check this fiddle 检查这个小提琴

You put DISTINCT IN sum( rating.rating) as total_rating, thats why the result( 12=17-5 ), since it will include 5 only once while computing sum. 你把DISTINCT sum( rating.rating) as total_rating,这就是为什么结果( 12 = 17-5 ),因为它在计算总和时只包含5次。

 select review.comments, review.user_id, count(distinct rating.id) as rating_count,
    sum( 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

Here is SQLFiddle 这是SQLFiddle

Sample Output : 样本输出: 在此输入图像描述 Hope this helps 希望这可以帮助

Try this - Remove the distinct from sum(rating.rating) . 试试这个 - 删除与sum(rating.rating)的区别。 Since you gave sum(distinct rating.rating) , it is ignoring one 5 that user 3 gave to store 1. 由于你给了sum(distinct rating.rating) ,它忽略了用户3给商店1的5。

select review.comments, users.username, count(distinct rating.id) as rating_count,
sum(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

First of all: It makes no sense to outer-join records from a table and then remove them in the WHERE clause. 首先:从表中外连接记录然后在WHERE子句中删除它们是没有意义的。 With left join review ... you say: find a matching record in table review, and if you don't find any, then add nulls, so we keep the users record. 使用left join review ...你说:在表评论中找到匹配的记录,如果你没有找到任何,那么添加空值,所以我们保留用户记录。 Then with where review.shop_id='1' you say: keep only records where you actually found a record in review. 然后where review.shop_id='1'你说:只记录你实际在审查中找到记录的记录。 So you are dismissing the records that you just took the pain to keep. 所以你要解雇那些你只是痛苦不堪的记录。 Your WHERE clause renders your LEFT OUTER JOINS mere INNER JOINS. 你的WHERE子句使你的LEFT OUTER加入仅仅INNER JOINS。

As to your actual problem: That stems from joining all tables first and only then trying to get aggregates from the resulting records. 至于你的实际问题:这源于首先加入所有表,然后尝试从结果记录中获取聚合。 Aggregate before joining instead: 在加入之前汇总:

select 
  rev.comments, 
  usr.username, 
  coalesce(rat.rating_count, 0) as rating_count,
  rat.total_rating 
from review rev
join users usr on users.id = review.user_id 
left join
(
  select user_id, shop_id, count(*) as rating_count, sum(rating) as total_rating
  from rating 
  group by user_id, shop_id
) rat on rat.user_id = usr.id and rat.shop_id = rev.shop_id
where rev.shop_id = 1 
group by rev.id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM