MySQL计算购买某个项目的唯一客户

时间:2016-10-07 12:37:18

标签: mysql

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    |
+-------+---------+---------+

有多少独特客户购买了苹果? 答案应该是: Apple 3

1 个答案:

答案 0 :(得分:1)

试试这个:

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