Domanda

I have a strange problem with mysql sum() result.

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

This query give me result: times = 10; result = 30;

But should give me result 10 too, because I have only one record in my db, with these conditions.

È stato utile?

Soluzione

you need GROUP BY . try add this

 GROUP BY  l.times 

like that

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

EDIT-:

  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 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top