2012-06-30 90 views
0

我能夠得到這個工作的查詢結果如下:對於每個結果,添加/合併爲一個單一的結果MySQL的

$query = "SELECT tt.time_start, tt.time_end 
FROM track_time as tt, project_track as pt, project_clients as pc, clients as cl 
WHERE tt.track_id = pt.track_id 
AND pt.project_id = pc.project_id 
AND pc.client_id = cl.client_id 
AND cl.client_id =".$db->prep($client_id); 
$res = $db->query($query,'assoc'); 

**RESULTS** 
time_start time_end client_id 
1340927940 1340928420 1 
1341066240 1341077040 1 
1341086400 1341088560 1 
1340954400 1340986800 7 

我試圖實現爲每一個客戶是將其中一個結果的所有time_start值加起來,將它們的所有time_end加起來,然後獲得這兩個結果之間的差異。這將允許我確定每個客戶的總數。

我正在使用PHP並嘗試各種方式的foreach,但沒有運氣和錯誤的數組返回。有任何想法嗎?

+0

'選擇CLIENT_ID,總和(TIME_START)-sum(TIME_END)爲 「差」,從MYTABLE組通過的client_id;' –

回答

1

嘗試這種情況:

$query = "SELECT pc.client_id, 
       (SUM(tt.time_start) - SUM(tt.time_end)) as `Difference` 
      FROM track_time as tt, project_track as pt, 
       project_clients as pc, clients as cl 
      WHERE tt.track_id = pt.track_id 
        AND pt.project_id = pc.project_id 
        AND pc.client_id = cl.client_id 
        AND cl.client_id =" . $db->prep($client_id) . " 
      GROUP BY client_id"; 
+0

完美!我顛倒了SUM(tt.time_start)SUM(tt.time_end)。謝謝! – jonthoughtit

0

time_starttime_end似乎是時間戳值。所以;

$total = 0; 
foreach($array as $item) 
{ 
    $total += strtotime($item['time_start']); 
} 

後,如果您運行intented格式date()功能,我認爲它應該正常工作。

+0

不工作。不返回任何東西。 – jonthoughtit

相關問題