2016-03-01 80 views
1

我有這樣 enter image description hereMySQL的SELECT多個值

創建谷歌圖表的表,所以我需要幾個DB查詢創建JSON。一切都完成了,但用數據庫查詢很難。

  1. 解決了此查詢。
  2. 需要選擇所有可開票時間和所有不可開票時間,其中的總和按日期分組。因此,要獲得每天的計費和不計費時間的總和。

目前,我有一個查詢,

$json = array(); 
$sql = "SELECT SUM(TIME_TO_SEC(`hours`)) as seconds,date FROM `am_am2_time_tracking` WHERE is_billable = 1 GROUP BY date"; 
$result = $conn->query($sql); 
while($row = $result->fetch_assoc()) 
{ 
    $bil_hours = floatval($row['seconds']/60/60); 
    $json[] = array('date'=>$row['date'],'bill_hours'=>$bil_hours); 
} 
echo json_encode($json); 

這給了我這個JSON:

[{"date":"2016-02-03","bill_hours":12.5},{"date":"2016-02-04","bill_hours":6},{"date":"2016-02-12","bill_hours":3},{"date":"2016-02-13","bill_hours":4},{"date":"2016-02-18","bill_hours":1.25},{"date":"2016-02-19","bill_hours":1},{"date":"2016-02-26","bill_hours":1.75},{"date":"2016-02-27","bill_hours":0.5}] 

但不是創造新的查詢,尋找更好的方式來在單個JSON兩小時。

+0

1.設法解決第一個與 $ SQL =「SELECT is_billable,小時,SUM(TIME_TO_SEC('hours'))作爲'Count' FROM 'am_am2_time_tracking' GROUP BY'is_billable'「; 但不能確定數字2. –

回答

0

你嘗試做下面的查詢

$sql = "SELECT SUM(TIME_TO_SEC(`hours`)) as seconds,date FROM `am_am2_time_tracking` GROUP BY date, is_billable"; 
+0

沒有運氣,結果相同。 –