0

How to find count of multiple columns in a table

create table foo (key1 int(11),  key2 int(11) ..)

I would like to do select count() for key1 and count() for key2 from foo in a single query, is it possible?

1
  • 1
    It will be the same for both, since every row has both columns. Commented Dec 16, 2014 at 2:45

1 Answer 1

1

If you want to count the distinct values you can do:

select count(distinct key1), count(distinct key2)
from foo;

If you want counts by values, you can use union all:

select key1, count(*)
from foo
group by key1
union all
select key2, count(*)
from foo
group by key2;
Sign up to request clarification or add additional context in comments.

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.