我有这些表:组通过查询挑战
customer
--------
customer_id int
name varchar(255)
order
-----
order_id int
customer_id int
discount boolean
我可以像查询得到每个客户做出订单数:
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
或者,我可以得到的数每个客户打折订单:
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
但我找不出一个方法来在一个单一的查询。我试过以下内容:
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
但它没有奏效。是否可以在单个(MySql)查询中计算两者?
干杯, 唐
在不具有IF函数,CASE表达式或DECODE系统函数可以用来代替。 – 2009-01-09 18:32:17