此例子主要是計算database每筆事件發生的時間總和

會使用到SUM, TIMESTAMPDIFF, TIMESTAMP


輸入:
mysql> select SUM(TIMESTAMPDIFF(second,`downTime`,`upTime`)) as totalTime from eventList where downTime >= TIMESTAMP(20011010223059) and upTime <= TIMESTAMP(20121020112233);



輸出:
+-----------+
| totalTime |
+-----------+
|   2616120 |
+-----------+
1 row in set (0.00 sec)




解釋:

SUM(): 將欄位加總!

TIMESTAMPDIFF(): 時間相減(可參考:http://openuedo.blogspot.tw/2012/04/mysqltimestampdiff-table-schema-is.html)

TIMESTAMP(): YYYYMMDDhhmmss (年年年年月月日日時時分分秒秒)

dreamtails 發表在 痞客邦 PIXNET 留言(0) 人氣()