0

I have stored three essential timestamps and times into my database table, format as follows:

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)

I need to put a few conditions like below:

difference = (receivedtime - requesttime);

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

legend:

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

I have checked UNIX_TIMESTAMP() of mysql, strtotime() of php and few other tries.

I also have checked several threads @ stackoverflow.com, but I can't find proper solution.

Any ideas...!

4

6 回答 6

2

检查TIMESTAMPDIFF()SEC_TO_TIME()功能:

SELECT
    `receivedtime`,
    `requesttime`,
    `sla`,
    (SEC_TO_TIME(TIMESTAMPDIFF(SECOND, `requesttime`, `receivedtime`)) < `sla`) as `meet`
FROM
    `table`;
于 2013-05-28T08:35:42.467 回答
1

尝试像这样从您的数据库中选择...

select TIMESTAMPDIFF(SECOND, receivedtime, requesttime) as difference, TIME_TO_SEC(sla) as slasecs from table

然后你可以做你的。

if [difference <= sla]
{
   show meet
} 
else 
{
   show don't meet
}
于 2013-05-28T08:55:51.487 回答
1

尝试

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

这是SQLFiddle演示

于 2013-05-28T08:46:19.930 回答
0

你可以在 SQL 中做到这一点

SELECT (UNIX_TIMESTAMP(receivedtime) - UNIX_TIMESTAMP(requesttime)) AS difference FROM table

它以秒为单位为您提供差异。

于 2013-05-28T08:35:24.250 回答
0

这是一个日期时间字段,而不是时间戳字段。您应该首先阅读时间戳是什么。

您可以通过转换为时间戳然后减去来找出差异。

$recieved = strtotime($res['receivedtime']);
$requested = strtotime($res['requesttime']);
$difference = $recieved - $requested;
if($difference <= $sla){
  //do stuff
}
于 2013-05-28T08:32:19.180 回答
0

尝试这个

select TIMESTAMPDIFF(SECOND,`requesttime`,`receivedtime`) as timeDiff FROM `table` 
WHERE TIMESTAMPDIFF(SECOND,`requesttime`,`receivedtime`) > TIME_TO_SEC(`sla`)

查看 SQL 小提琴

你可以在mysql文档中找到相关的

于 2013-05-28T08:49:43.107 回答