#兩日期/時間之間相差的天數:
To_Days(end_time)-To_Days(start_time)
#兩日期/時間之間相差的秒數:
SELECT UNIX_TIMESTAMP(end_time)- UNIX_TIMESTAMP(start_time);
#兩日期/時間之間相差的時分數:
SELECT SEC_TO_TIME(UNIX_TIMESTAMP(end_time)- UNIX_TIMESTAMP(start_time));
最好的辦法是直接用「+」,「-」 計算,目前的版本都支持了。
好比:SELECT * FROM Table_Name WHERE loginDate < NOW() -10 * 60;sql
實例:spa
SELECT SEC_TO_TIME(UNIX_TIMESTAMP((SELECT createdtime FROM `Z_2015-09-15` WHERE deviceid='460040271516782' order by id DESC limit 1)) - UNIX_TIMESTAMP((SELECT createdtime FROM `Z_2015-09-15` WHERE deviceid='460040271516782' limit 1)));