MySQL通过where子句计算多行

时间:2012-12-02 20:22:09

标签: mysql

我正在编写一组类似的查询:

SELECT count(users.id) AS `id` FROM users WHERE `vote` = 'cont6'
SELECT count(users.id) AS `id` FROM users WHERE `vote` = 'cont7'
SELECT count(users.id) AS `id` FROM users WHERE `vote` = 'cont8'

我想知道如何将这个用于单个查询?所以我得到一个类似于以下的结果集:

cont6 = 12
cont7 = 4
cont8 = 15

非常感谢! :)

1 个答案:

答案 0 :(得分:2)

SELECT vote, count(users.id) AS `id` FROM users GROUP BY vote