tbl_customer.id is the customer's id
tbl_order.customer_id
a query that will select all customers with say 4 or more records in tbl_order
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.