2014-02-09 53 views
2

我有如下表:PHP和MySQL:查詢到 「和」 2個從PHP

+------------+-------+------------+-----------+ 
| date  | time | custom_id | Number | 
+------------+-------+------------+-----------+ 
| 01/17/2014 | 00:00 | ABC123  | 15  | 
| 01/17/2014 | 00:00 | HEY567  | 25  | 
| 01/17/2014 | 00:30 | ABC123  | 17  | 
| 01/17/2014 | 00:30 | HEY567  | 28  | 
| 01/17/2014 | 01:00 | ABC123  | 17  | 
| 01/17/2014 | 01:00 | HEY567  | 28  | 
| 01/17/2014 | 01:30 | ABC123  | 17  | 
| 01/17/2014 | 01:30 | HEY567  | 28  | 
+------------+-------+------------+-----------+ 

我怎樣才能和2行(總數)具有相同CUSTOM_ID & DATE當給定的時間只是:30分鐘的差異將它舍入到:00並刪除:30?它應該看起來像這樣到底:

+------------+-------+------------+-----------+ 
| date  | time | custom_id | Number | 
+------------+-------+------------+-----------+ 
| 01/17/2014 | 00:00 | ABC123  | 32  | 
| 01/17/2014 | 00:00 | HEY567  | 53  | 
| 01/17/2014 | 01:00 | ABC123  | 34  | 
| 01/17/2014 | 01:00 | HEY567  | 56  | 
+------------+-------+------------+-----------+ 

我該如何使用PHP或可能的MySQL查詢來完成此操作?

在此先感謝

SQLFiddle:http://sqlfiddle.com/#!2/cc529/1

+0

你能提供sqlfiddle,這樣我們就可以測試解決方案? – user4035

+0

當然我會立即創建它 – user3288852

+0

這裏是SQLFiddle:http://sqlfiddle.com/#!2/cb8efb/1 – user3288852

回答

1

這應該工作

SELECT date, MIN(time) as time, custom_id, SUM(Number) as Number 
FROM table 
GROUP BY date, custom_id, HOUR(time) 
+0

WOW。我很驚訝它是多麼簡單,謝謝你剛剛救了我的工作!如果其他人需要,最終結果如下:http://sqlfiddle.com/#!2/913b4/1 – user3288852