結合數據是否有辦法讓我得到一個不同的名稱,那麼在今天,明天的數量,這些查詢組合,和後天的MySQL從三個查詢
select name, SUM(qty) as qty_today, 0 as qty_tomororow, 0 as qty_om
from order_item
where delivery_date = '2011-11-22'
group by name
union all
select name, qty as qty_today, SUM(qty) as qty_tomororow, 0 as qty_om
from order_item
where delivery_date = '2011-11-23'
group by name
union all
select name, 0 as qty_today, 0 as qty_tomororow, SUM(qty) as qty_om
from order_item
where delivery_date = '2011-11-24'
group by name
UPDATE
鑑於表
名稱,數量,DELIVERY_DATE
芝加哥,1,2011-11-22
紐約,2,2011-11-22
芝加哥,3,2011-11-23
我希望
芝加哥,1,3,0
紐約,1,0,0
這就是說,看起來你應該`GROUP BY DELIVERY_DATE,name`代替。 – Kenaniah 2011-12-14 20:26:04