Cyclone
Cyclone

Reputation: 15269

Sum rows with multiple ID

I'm using this query:

SELECT SUM(voc=1) AS s, SUM(voc=2) AS d, SUM(voc=3) AS p FROM `pl`;

But now I need to modify this query, so it'll SUM when voc is equal to the multiple numbers:

SELECT SUM ( voc = 1 OR voc = 7 OR voc = 3 ) as `s` FROM `pl`;

Unfortunately , the above is not working properly, I've read that this can be done using CASE, but my knowledge about that is too small...

Upvotes: 0

Views: 1499

Answers (1)

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10512

SELECT SUM ( CASE WHEN voc = 1 OR voc = 7 OR voc = 3 THEN 1 ELSE 0 END) as `s` FROM `pl`;

UPD As stated by @Raul in comment - WHERE should be much more effective, like this:

SELECT COUNT(*) AS `s` FROM `p1` WHERE voc IN (1,3,7);

UPD2 Changed Sum's to Count's - thanks to @newtower

Upvotes: 3

Related Questions