我有三个mySQL表:


客户
产品
采购


我想让三个拥有ID = 1的产品最大购买量的客户:

client:-

client_id | name
1         | client1
2         | client2
3         | client3
4         | client4

product:-
    prod_id | name
    1         | product1


采购:

purchase_id | client_id | product_id
1           | 1         |1
2           | 1         |1
3           | 1         |1
4           | 1         |1
5           | 2         |1
7           | 2         |1
8           | 2         |1
9           | 3         |1
10          | 3         |1
11          | 4         |1


=>我想得到以下结果:

-client1

-client2

-client3

我能怎么做?

最佳答案

SELECT client.name
FROM client
JOIN purchase ON purchase.client_id = client.client_id
JOIN product ON purchase.product_id = product.product_id and product.product_id = 1
GROUP BY client.client_id
ORDER BY COUNT(purchase.purchase_id) DESC
LIMIT 3

关于mysql - MySQL,获得购买量最大的三个客户:,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24861363/

10-10 19:11