内联合与sum和group by结合使用

时间:2017-07-20 17:43:18

标签: mysql sql

我在下面的SQL语句中遇到了一些奇怪的问题。结果按用户ID分组,其中一些结果正确,但对于其中一个(用户ID = 1)" initial_average"乘以3.我真的不知道为什么......声明的结构有什么问题吗?如果不清楚,目的是总结字段" initial_avg"在"任务"表,并由用户分解。对此有一些帮助非常感谢。我正在使用MySQL。

SELECT sum(initial_avg) AS initial_average
     , sum(initial_std) AS initial_standard_dev
     , tasks.user
     , hourly_rate 
FROM tasks 
INNER JOIN user_project 
   ON tasks.user=user_project.user 
  AND tasks.project=59 
  AND tasks.user=1 
GROUP BY tasks.user

2 个答案:

答案 0 :(得分:0)

我刚刚通过添加另一个“和”子句来解决它(AND user_project.project = 59)

答案 1 :(得分:0)

优化您的查询(试一试):

SELECT SUM(initial_avg) AS initial_average, SUM(initial_std) AS initial_standard_dev, tasks.user, hourly_rate FROM tasks INNER JOIN user_project ON tasks.user = user_project.user AND tasks.project = User_project.project WHERE tasks.project = 59 AND tasks.user = 1 GROUP BY tasks.user, hourly_rate