2013-05-28 80 views
0

我有一個表列「工作時間」,我想總結的所有行是這樣的:總和小時總行

08:00 
    02:00 
    03:30 
Total: 13:30 

這是我的SQL SELECT代碼

$sql=mysql_query("SELECT * FROM sohy_works WHERE data_raport = '$data' AND codangajat = '$codangajat' "); 
+0

是所選答案真的解決問題嗎? – Danijel

回答

3

你可以計算出這個樣子,

SELECT SEC_TO_TIME(SUM(your_time_field)) FROM sohy_works WHERE ..... 
+0

我可以做這個「SELECT *(ALL)SEC_TO_TIME(SUM(your_time_field))從sohy_works WHERE .....? –

2

的從數據庫中獲取秒之和將其轉換成小時和分鐘:

$q = "SELECT SUM(TIME_TO_SEC(worked_hours)) FROM sohy_works WHERE data_raport = '$data' AND codangajat = '$codangajat'"; 

$result = mysql_query($q); 
$sec = mysql_fetch_array($result); 

$hours = floor($sec[0]/3600); 
$minutes = str_pad(floor($sec[0] % 3600/60), 2, '0', STR_PAD_LEFT); 

echo $hours.':'.$minutes; 

編輯:

小時和分鐘可直接與SQL查詢等計算:

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`worked_hours`))) FROM ...