奇怪的SUM()结果

时间:2014-05-03 11:45:28

标签: mysql sql sum

我对mysql sum()结果有一个奇怪的问题。

SELECT  `users_limits`.times, SUM( l.times ) AS  `result` 
    FROM  `users_limits` 
      INNER JOIN  `users_limits` AS  `l` 
      INNER JOIN  `vacation_types` AS  `v` ON l.id_vacation_type = v.id_vacation_type
   WHERE  l.year =2014 AND v.type =0

此查询给我结果: 次= 10; 结果= 30;

但是也应该给我结果10,因为我的数据库中只有一条记录,具有这些条件。

1 个答案:

答案 0 :(得分:0)

你需要GROUP BY。尝试添加此

 GROUP BY  l.times 
像那样

   WHERE l.year =2014 AND v.type =0
   GROUP BY  l.times 

编辑 - :

  SELECT  `users_limits`.times, SUM( l.times ) AS  `result` 
  FROM  `users_limits` l
  INNER JOIN  `vacation_types` AS  `v` ON l.id_vacation_type = v.id_vacation_type
  WHERE l.year =2014 AND v.type =0
  GROUP BY  l.times