Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql select count values from a single column

Tags:

mysql

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?

like image 902
Meg Lepett Avatar asked Dec 20 '22 01:12

Meg Lepett


2 Answers

Use group by:

SELECT `key`, COUNT(*) FROM `test` GROUP BY `key`;
like image 173
fabien Avatar answered Jan 25 '23 18:01

fabien


you can do like this

select count(key) as K FROM test where key in (1,2,3)
like image 44
Aman Varshney Avatar answered Jan 25 '23 18:01

Aman Varshney