0

我需要找出有多少用户带来了产品(注意:我不需要用户总数,只需要多少用户带来)。

表名:采购员

+-----+----------+---------+
| id  | products |    uid  |
|-----|----------|---------|
| 1   |  soap    |    2    |
| 2   |  h_oil   |    1    |
| 3   |  soap    |    3    |
| 4   |  tea     |    1    |
| 5   |  soap    |    2    |
| 6   |  h_oil   |    1    |
| 7   |  soap    |    3    |
| 8   |  tea     |    1    |
+-----+----------+---------+

我需要如下表所示的结果:

|-----|---------------|--------------|
| id  | product_name  |  total_user  |
|-----|---------------|--------------|
| 1   |  soap         |      2       |
| 2   |  h_oil        |      1       |
| 3   |  tea          |      1       |
|------------------------------------|

如何在 MySQL 查询中做到这一点?

4

5 回答 5

1

你可以使用:

SELECT product_name, COUNT(*) AS total_user FROM buyer GROUP BY product_name;
于 2013-05-07T12:38:09.537 回答
1
 SELECT products product_name, COUNT(DISTINCT uid) total_user FROM purchases GROUP BY products;
于 2013-05-07T12:44:03.580 回答
0

像这样:

SELECT 
    COUNT(*) 
FROM
    buyer
GROUP BY
    products 
于 2013-05-07T12:37:35.570 回答
0

简而言之,您正在寻找一个group by声明

SELECT uid, product_name, count(id) as total_user FROM buyer GROUP BY uid

在这里阅读更多:

于 2013-05-07T12:38:31.163 回答
0
SELECT 
    COUNT(id) 
FROM
    buyer
GROUP BY
    products 
于 2013-05-07T12:38:49.117 回答