简体   繁体   中英

Mysql select count values from a single column

A have a column named key - 1,1,2,2,2,2,3 Now i do it with 3 querys:

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

How to count in one query how many 1,2,3?

使用分组:

SELECT `key`, COUNT(*) FROM `test` GROUP BY `key`;

你可以这样做

select count(key) as K FROM test where key in (1,2,3)

This is another option:

SELECT sum(key=1) as k1, sum(key=2) as k2,sum(key=3) as k3 FROM `test`

you could also add a group by column if the values of key were part of some other group.

Try this

SELECT  COUNT(Key) as K
        FROM 
        test 
        GROUP BY Key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM