Well, I deleted my previous answer because finally it was not what Williamford was looking for, but I said that maybe we all did not understand the question.
I also thought about SELECT DISTINCT... at first SELECT DISTINCT... but it seemed a little strange to me that someone had to know how many people had a different number of pets than the rest ... that's why I thought maybe the question is was not clear enough.
So, now that the meaning of the real question is being clarified by making a subquery for this rather overhead, I would rather use the GROUP BY .
Imagine you have a customer_pets table as follows:
+-----------------------+ | customer | pets | +------------+----------+ | customer1 | 2 | | customer2 | 3 | | customer3 | 2 | | customer4 | 2 | | customer5 | 3 | | customer6 | 4 | +------------+----------+
then
SELECT count(customer) AS num_customers, pets FROM customer_pets GROUP BY pets
will return:
+----------------------------+ | num_customers | pets | +-----------------+----------+ | 3 | 2 | | 2 | 3 | | 1 | 4 | +-----------------+----------+
as you need.
maid450 Jan 18 '11 at 19:30 2011-01-18 19:30
source share