A有一个名为key的列 - 1,1,2,2,2,2,3 现在我用3个查询来做:
SELECT count(key) as k FROM `test` WHERE key=1
SELECT count(key) as k FROM `test` WHERE key=2
SELECT count(key) as k FROM `test` WHERE key=3
如何在一个查询中统计有多少1,2,3?
答案 0 :(得分:5)
使用group by:
SELECT `key`, COUNT(*) FROM `test` GROUP BY `key`;
答案 1 :(得分:1)
你可以这样做
select count(key) as K FROM test where key in (1,2,3)
答案 2 :(得分:1)
这是另一种选择:
SELECT sum(key=1) as k1, sum(key=2) as k2,sum(key=3) as k3 FROM `test`
如果key的值是某个其他组的一部分,您还可以按列添加组。
答案 3 :(得分:0)
试试这个
SELECT COUNT(Key) as K
FROM
test
GROUP BY Key