Mysql和php如果时间差异小于1分钟

时间:2016-08-12 16:12:30

标签: php mysql time

在mysql中我有很多次,我用PHP表示。

现在我需要, 如果时间,我表示,在一分钟内(差异)我想在(html)表中显示红色的时间。

    Row    |    Time
     1     |  10:10:11
     2     |  10:12:01   <--- THIS RED
     3     |  10:12:50   <--- THIS RED
     4     |  10:14:12

有什么方法可以用PHP做到这一点? 现在我使用一个简单的mysql查询。选择....从....

我很感激所有事情

2 个答案:

答案 0 :(得分:1)

SELECT t1.Row,
       t1.Time,
       CASE WHEN t2.minute IS NOT NULL THEN 'Red' ELSE 'Black' END AS Color
FROM yourTable t1
LEFT JOIN
(
    SELECT CONCAT(SUBSTRING(Time, 1, 2), SUBSTRING(Time, 4, 2)) AS minute
    FROM yourTable
    GROUP BY CONCAT(SUBSTRING(Time, 1, 2), SUBSTRING(Time, 4, 2))
    HAVING COUNT(*) > 1
) t2
    ON CONCAT(SUBSTRING(t1.Time, 1, 2), SUBSTRING(Time, 4, 2)) = t2.minute

答案 1 :(得分:1)

试试这个:

SELECT t1.`Row`,
       t1.`Time`,
       TIME_TO_SEC(TIMEDIFF(t2.`Time`, t1.`Time`)) AS Diff,
       CASE WHEN (TIME_TO_SEC(TIMEDIFF(t2.`Time`, t1.`Time`)) < 61 || TIME_TO_SEC(TIMEDIFF(t1.`Time`, t3.`Time`)) < 61) THEN 'Red' ELSE 'Blue' END AS Color
FROM myTable t1
LEFT JOIN myTable t2
    ON t2.`Row` = t1.`Row`+1
LEFT JOIN myTable t3
    ON t3.`Row` = t1.`Row`-1
    ORDER BY t1.`Row` ASC

FIDDLE DEMO

请在我的演示中考虑,我使用VARCHAR for Time。但是,如果您使用任何其他类型,这并不重要。查询有效。