5

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?

1

4 Answers 4

10

Use group by:

SELECT `key`, COUNT(*) FROM `test` GROUP BY `key`;
Sign up to request clarification or add additional context in comments.

Comments

1

you can do like this

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

1 Comment

otherwise you can use groupby
1

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.

Comments

0

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.