我正在使用此查询:
SELECT SUM(voc=1) AS s, SUM(voc=2) AS d, SUM(voc=3) AS p FROM `pl`;
但是现在我需要修改此查询,因此当SUM
等于多个数字时它会voc
:
SELECT SUM ( voc = 1 OR voc = 7 OR voc = 3 ) as `s` FROM `pl`;
不幸的是,以上操作不正常,我已经读过这可以使用CASE
完成,但我对此的了解太少了......
答案 0 :(得分:3)
SELECT SUM ( CASE WHEN voc = 1 OR voc = 7 OR voc = 3 THEN 1 ELSE 0 END) as `s` FROM `pl`;
UPD 正如@Raul在评论中指出的那样 - WHERE
应该更加有效,如下所示:
SELECT COUNT(*) AS `s` FROM `p1` WHERE voc IN (1,3,7);
UPD2 将总和更改为计数 - 感谢@newtower