tags:

views:

39

answers:

3

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

+1  A: 
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;
a1ex07
+2  A: 
SELECT tbl_order.customer_id, COUNT(*)
    FROM tbl_order 
    GROUP BY customer_id 
    HAVING COUNT(*) > 4
Larry Lustig
A: 

Hi 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;
zzapper
tbl_order_head has a date field "created" can I rejig above query to list in order of most recent sale/display date of most recent sale
zzapper