2
    SELECT imagename, SUM(ratingvalue) as "lol"
    FROM ratings
    GROUP BY imagename;

How do I go about grabbing the total of a specific imagename so i can then display it onto the page, so for example instead of returning the results for all 12 image name's i want to be able to limit that down to one specific imagename. I would be able to do this if I could use the WHERE statement but I realise you cannot do that with aggregate functions.

Any ideas on how I can implement WHERE imagename = '{$imgname}' in the above SQL query?

Basically I'm looking for an alternate method to WHERE.

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Emmett
  • 357
  • 2
  • 4
  • 15
  • You can use WHERE within a query which is aggregating, but the WHERE will filter for criteria pre-aggregation. HAVING will filter post-aggregation. In this case, not sure it matters much, tbh. – VBlades Jun 29 '14 at 21:15

2 Answers2

4
SELECT imagename, SUM(ratingvalue) as "lol"
FROM ratings
WHERE imagename = 'MyImageName'
GROUP BY imagename;
FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
0

Try HAVING - it's like WHERE but after aggregation.

SELECT imagename, SUM(ratingvalue) as "lol"
FROM ratings
GROUP BY imagename
HAVING imagename = 'MyImageName';
VBlades
  • 2,241
  • 1
  • 12
  • 8