MySQL选择购买最多的客户名称

时间:2019-06-18 07:09:31

标签: mysql

图片中我有5张桌子彼此相连

[![Image1] [1]] [1]

Image2

我尝试编写查询

style = {{ elevation: 0 }}

对于大多数购买,我应该考虑什么参数?

4 个答案:

答案 0 :(得分:1)

您可以通过调整select子句(删除order_id)来解决此问题。 然后,您将需要一个HAVING子句来实现该条件,以仅显示购买订单最多的客户。

答案 1 :(得分:0)

SELECT lastname,firstname
FROM customer
INNER JOIN purchase_order
ON customer.customer_id=purchase_order.customer_id
GROUP BY customer_id 
ORDER BY COUNT(order_id) DESC LIMIT 1;

答案 2 :(得分:0)

select lastname, firstname from customer c
inner join (
select customer_id, count(1) from purchase_order
group by 1
order by 2 desc limit 1) most 
/* the above sub-query named as 'most' fetched the customer id with the maximum 
number of orders */
on c.customer_id = most.customer_id; -- this is a simple join of customers table with the table generated from sub-query above

答案 3 :(得分:0)

SELECT c.firstname,c.lastname
FROM customer c
JOIN purchase_order po
ON c.customer_id = po.customer_id
GROUP BY c.firstname,c.lastname
ORDER BY COUNT(po.order_id) DESC LIMIT 1;