Hi,
I have these tables:
customer
--------
customer_id int
name varchar(255)
order
-----
order_id int
customer_id int
discount boolean
I can get the number of orders made by each customer with a query like:
select c.id, count(o.order_id)
from customer c
left join order as o using c.customer_id = o.customer_id
group by 1
Alternatively, I can get the number of discounted orders made by each customer with:
select c.id, count(o.order_id)
from customer c
left join order as o using c.customer_id = o.customer_id and o.discount = true
group by 1
But I can't figure out a way to get both in a single query. I've tried the following:
select c.id, count(o.order_id), count(o2.order_id)
from customer c
left join order as o using c.customer_id = o.customer_id
left join order as o2 using c.customer_id = o2.customer_id and o2.discount = true
group by 1
But it didn't work. Is it possible to calculate both in a single (MySql) query?
Cheers, Don