我有一个表,其结构如下
+---------------------+---------+
| date_time | portNo1 |
+---------------------+---------+
| 2016-01-08 13:15:46 | 1 |
| 2016-01-08 13:16:01 | 2 |
| 2016-01-08 13:16:16 | 1 |
| 2016-01-08 13:16:31 | 2 |
| 2016-01-08 13:16:46 | 1 |
| 2016-01-08 13:17:00 | 2 |
| 2016-01-08 13:17:16 | 1 |
| 2016-01-08 13:17:31 | 1 |
| 2016-01-08 13:17:46 | 0 |
| 2016-01-08 13:18:01 | 0 |
| 2016-01-08 13:18:16 | 0 |
| 2016-01-08 13:18:31 | 0 |
| 2016-01-08 13:18:41 | 0 |
| 2016-01-08 13:19:01 | 0 |
| 2016-01-08 13:19:16 | 0 |
| 2016-01-08 13:19:27 | 0 |
| 2016-01-08 13:19:41 | 0 |
| 2016-01-08 13:19:56 | 0 |
| 2016-01-08 13:20:11 | 0 |
| 2016-01-08 13:20:26 | 0 |
| 2016-01-08 13:20:41 | 0 |
| 2016-01-08 13:20:56 | 0 |
| 2016-01-08 13:21:16 | 0 |
| 2016-01-08 13:21:31 | 0 |
| 2016-01-08 13:21:43 | 0 |
| 2016-01-08 13:22:01 | 0 |
| 2016-01-08 13:22:16 | 0 |
| 2016-01-08 13:22:31 | 0 |
| 2016-01-08 13:22:46 | 0 |
| 2016-01-08 13:23:01 | 0 |
| 2016-01-08 13:23:12 | 0 |
| 2016-01-08 13:23:31 | 0 |
| 2016-01-08 13:23:46 | 2 |
| 2016-01-08 13:24:01 | 2 |
| 2016-01-08 13:24:16 | 2 |
| 2016-01-08 13:24:31 | 1 |
| 2016-01-08 13:24:46 | 2 |
| 2016-01-08 13:25:00 | 2 |
现在我要做的只是添加portNo1
值不是0
的时间
这意味着它应该在几秒钟内将时间从13:15:46
添加到13:17:31
,然后在所有记录到13:23:46
值为0
之后,它应该忽略该时间并继续。然后从13:23:46
再次portNo1
值13:23:46
和13:23:31
之间的区别,并将其添加到之前的差异中。伙计们我在perl中尝试了很多但是没有得到正确的值,如果在mysql中有任何简单的方法请告诉我。
答案 0 :(得分:1)
除非我理解所有错误的问题,否则您可以执行以下操作:
SELECT SUM(UNIX_TIMESTAMP(date_time)-UNIX_TIMESTAMP(prevtime))
FROM (
SELECT date_time, portNo1, @prevTime as prevtime,
@prevTime := date_time
FROM MyTable
) t1
WHERE (portNo1 > 0 AND prevtime IS NOT null)
如果当前行中的portNo1
大于0,这将汇总当前行与上一行之间的时差(以秒为单位)。这是您想要的吗?
我在这里做了一个小提琴:http://sqlfiddle.com/#!9/b6dfa/3