2016-02-05 80 views
0

眼下返回數組,我有這個疑問:如何同時使用GROUP BY

SELECT COUNT(*) AS Count, SUM(Ask) AS Ask, SUM(Cost) AS Cost, Provider, Factura FROM store_items 
    WHERE (
     Provider NOT IN(SELECT Provider FROM store_provider_invoices) 
     AND Factura NOT IN(SELECT Factura FROM store_provider_invoices) 
    ) 
    OR Factura NOT IN(SELECT Factura FROM store_provider_invoices) 
    GROUP BY Provider, Factura 

這是偉大的工作,並返回以下陣列:

Array ( 
    [0] => Array ( 
     [Count] => 1 
     [ID] => 13 
     [Ask] => 20.00 
     [Cost] => 10.00 
     [Provider] => 5 
     [Factura] => 8 
    ) 
    [1] => Array ( 
     [Count] => 1 
     [ID] => 18 
     [Ask] => 125.01 
     [Cost] => 110.01 
     [Provider] => 5 
     [Factura] => 34 
    ) 
    [3] => Array ( 
     [Count] => 3 
     [ID] => 14 
     [Ask] => 210.00 
     [Cost] => 150.00 
     [Provider] => 6 
     [Factura] => 5 
    ) 
) 

我想怎麼辦是也返回所有匹配來自store_items表的查詢,像ID的:

Array ( 
    [0] => Array ( 
     [ID] => Array (
     [0] => 101 
    ) 
     [Count] => 1 
     [Ask] => 20.00 
     [Cost] => 10.00 
     [Provider] => 5 
     [Factura] => 8 
    ) 
    [1] => Array ( 
     [ID] => Array (
     [0] => 102 
    ) 
     [Count] => 1 
     [Ask] => 125.01 
     [Cost] => 110.01 
     [Provider] => 5 
     [Factura] => 34 
    ) 
    [3] => Array ( 
     [ID] => Array (
     [0] => 103 
     [1] => 104 
     [2] => 105 
    ) 
     [Count] => 3 
     [Ask] => 210.00 
     [Cost] => 150.00 
     [Provider] => 6 
     [Factura] => 5 
    ) 
) 

因此,F或實例,在上面的最後一個數組元素中,而不是僅返回3的Count,還返回它計算的每一行的ID。

回答