NickC NickC - 2 months ago 7
MySQL Question

MySQL count unique customer that purchased an certain item

tbl_products

+-------+---------+---------+
| id | item | Customer|
+-------+---------+---------+
| 1 | Apple | brad |
| 2 | Apple | alex |
| 3 | Apple | brad |
| 4 | Apple | brad |
| 5 | Apple | tim |
| 6 | Orange | brad |
| 7 | Orange | steve |
| 8 | Orange | alex |
+-------+---------+---------+


How many unique customers purchased Apples?
answer should be:
Apple 3

Answer

Try this:

SELECT COUNT(DISTINCT Customer)
FROM tbl_products
WHERE  item ='Apple'