我已经在数据库表中存储了三个基本的时间戳和时间,格式如下:

mysql> select receivedtime, requesttime, sla from table;
+---------------------+---------------------+----------+
| receivedtime        | requesttime         | sla      |
+---------------------+---------------------+----------+
| 2013-05-26 22:37:04 | 2013-05-26 12:37:04 | 02:59:59 |
| 2013-05-26 14:36:44 | 2013-05-21 12:39:09 | 72:00:00 |
+---------------------+---------------------+----------+
2 rows in set (0.00 sec)


我需要提出以下一些条件:

difference = (receivedtime - requesttime);

if [difference <= sla]
{
   show meet
} else {
   show don't meet
}


传说:

receivedtime [timestamp] as because days
requesttime [timestamp] as because days
sla [time] as because hour


我检查了UNIX_TIMESTAMP()mysql, strtotime()php以及其他一些尝试。

我也检查了几个线程@ stackoverflow.com,但是找不到合适的解决方案。

有任何想法吗...!

最佳答案

尝试

SELECT receivedtime,
       requesttime,
       CASE WHEN TIMESTAMPDIFF(SECOND, requesttime, receivedtime) < TIME_TO_SEC(sla)
            THEN 'meet' ELSE 'don\'t meet' END sla
  FROM table1


这是SQLFiddle演示

08-06 21:56