[英]MySQL query to find customers who have made the most orders
tbl_customer.id is the customer's id tbl_customer.id是客户的ID
tbl_order.customer_id tbl_order.customer_id
a query that will select all customers with say 4 or more records in tbl_order 一个查询将选择tbl_order中具有4个或更多记录的所有客户
SELECT tbl_order.customer_id, COUNT(*)
FROM tbl_order
GROUP BY customer_id
HAVING COUNT(*) > 4
select customer.id, count(*) as num_orders
from customer
inner join tbl_order ON (tbl_order.customer_id = customer.id)
group by customer.id
having num_orders >= 4;
Using a combination of above answers (thanks) 使用上述答案的组合(谢谢)
What I ended up with 我最终得到了什么
SELECT c.first_name,c.SURNAME,c.ADDRESS1,c.CREATED ,COUNT(*) AS num_orders
FROM tbl_customer AS c
INNER JOIN tbl_order_head AS o ON (o.customer_id = c.id)
GROUP BY c.id
HAVING num_orders >= 4 ORDER BY num_orders DESC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.