简体   繁体   English

在MySQL中列为0的持续时间之间选择时间戳记值

[英]select timestamp values between time durations where a column has 0 in MySQL

I want to select the time limits(stamp_date) when the uptime has 0 or continues to be in 0. 我想选择正常运行时间为0或继续为0的时间限制(stamp_date)。

TABLE: 表:

CREATE TABLE IF NOT EXISTS sample (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    sname VARCHAR(30) NULL,
    uptime BIGINT,
    stamp_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

INSERT statements: INSERT语句:

insert into sample values(null,'hi',10,'2015-01-08 05:30:00');
insert into sample values(null,'hi',20,'2015-01-08 05:40:00');
insert into sample values(null,'hi',30,'2015-01-08 05:50:00');
insert into sample values(null,'hi',40,'2015-01-08 06:00:00');
insert into sample values(null,'hi',50,'2015-01-08 06:10:00');
insert into sample values(null,'hi',0,'2015-01-08 06:20:00');
insert into sample values(null,'hi',10,'2015-01-08 06:30:00');
insert into sample values(null,'hi',20,'2015-01-08 06:40:00');
insert into sample values(null,'hi',30,'2015-01-08 06:50:00');
insert into sample values(null,'hi',40,'2015-01-08 07:00:00');
insert into sample values(null,'hi',0,'2015-01-08 07:10:00');
insert into sample values(null,'hi',0,'2015-01-08 07:20:00');
insert into sample values(null,'hi',0,'2015-01-08 07:30:00');
insert into sample values(null,'hi',0,'2015-01-08 07:40:00');
insert into sample values(null,'hi',0,'2015-01-08 07:50:00');
insert into sample values(null,'hi',10,'2015-01-08 08:00:00');
insert into sample values(null,'hi',20,'2015-01-08 08:10:00');
insert into sample values(null,'hi',0,'2015-01-08 08:20:00');
insert into sample values(null,'hi',40,'2015-01-08 08:30:00');

sample table: 样品表:

mysql> select * from sample;
+----+-------+-------+---------------------+
| id | sname | uptime| stamp_date          |
+----+-------+-------+---------------------+
|  1 | hi    |    10 | 2015-01-08 05:30:00 |
|  2 | hi    |    20 | 2015-01-08 05:40:00 |
|  3 | hi    |    30 | 2015-01-08 05:50:00 |
|  4 | hi    |    40 | 2015-01-08 06:00:00 |
|  5 | hi    |    50 | 2015-01-08 06:10:00 |
|  6 | hi    |     0 | 2015-01-08 06:20:00 |
|  7 | hi    |    10 | 2015-01-08 06:30:00 |
|  8 | hi    |    20 | 2015-01-08 06:40:00 |
|  9 | hi    |    30 | 2015-01-08 06:50:00 |
| 10 | hi    |    40 | 2015-01-08 07:00:00 |
| 11 | hi    |     0 | 2015-01-08 07:10:00 |
| 12 | hi    |     0 | 2015-01-08 07:20:00 |
| 13 | hi    |     0 | 2015-01-08 07:30:00 |
| 14 | hi    |     0 | 2015-01-08 07:40:00 |
| 15 | hi    |     0 | 2015-01-08 07:50:00 |
| 16 | hi    |    10 | 2015-01-08 08:00:00 |
| 17 | hi    |    20 | 2015-01-08 08:10:00 |
| 18 | hi    |     0 | 2015-01-08 08:20:00 |
| 19 | hi    |    40 | 2015-01-08 08:30:00 |
+----+-------+-------+---------------------+
19 rows in set (0.00 sec)

1) The link was down (uptime=0) between 06:20:00 to 06:30:00 1)在06:20:00到06:30:00之间,链接已断开(uptime = 0)

2) The link was down (uptime=0) between 07:10:00 to 08:00:00 2)链接在07:10:00到08:00:00之间断开(正常运行时间= 0)

3) The link was down (uptime=0) between 08:20:00 to 08:30:00 3)在08:20:00到08:30:00之间链接断开(正常运行时间= 0)

Expected result should be like the below: 预期结果应如下所示:

+++++++++++++++++++++++++
| When Uptime is 0      |
+++++++++++++++++++++++++
|  06:20:00 to 06:30:00 |
|  07:10:00 to 08:00:00 |
|  08:20:00 to 08:30:00 |
+++++++++++++++++++++++++ 

Could someone please help me to write a SQL or stored procedure to achieve the above result? 有人可以帮我写一个SQL或存储过程来达到上述效果吗?

Thanks, 谢谢,

Yogesh 约杰什

If your IDs have no holes in them (eg 18 is not followed by 20), then the following query will do the trick: 如果您的ID中没有漏洞(例如18后面没有20),则可以使用以下查询:

select concat(time(r.stamp_date), ' to ', 
       IFNULL((select time(min(stamp_date))
       from sample
       where id > r.id and uptime != 0
       ), "NOW")) `When Uptime is 0`
from sample l 
     join
     sample r 
     on l.id = r.id - 1
where l.uptime != 0 and r.uptime = 0;

This is what it returns for your data with an extra downtime entry at the end 这是它为您的数据返回的内容,最后还有一个额外的停机时间条目

+----------------------+ | When Uptime is 0 | +----------------------+ | 06:20:00 to 06:30:00 | | 07:10:00 to 08:00:00 | | 08:20:00 to 08:30:00 | | 08:40:00 to NOW | +----------------------+ 4 rows in set (0.00 sec)

If your IDs do have holes, then you'll need to modify the ON condition a little bit 如果您的ID确实有孔,则需要稍微修改ON条件

select concat(time(r.stamp_date), ' to ', 
       IFNULL((select time(min(stamp_date))
       from sample
       where id > r.id and uptime != 0
       ), "NOW")) `When Uptime is 0`
from sample l 
     join
     sample r 
     on l.id = (select max(id) from sample where id < r.id)
where l.uptime != 0 and r.uptime = 0;

check this out on fiddle here is fiddle 在小提琴上检查一下这是小提琴

SELECT 
  CONCAT(
    DATE_FORMAT(a.stamp_date, '%H:%i:%s'),
    ' to ',
    DATE_FORMAT(b.stamp_date, '%H:%i:%s')
  ) AS 'When Uptime is 0' 
FROM
  (SELECT 
    id,
    stamp_date 
  FROM
    sample 
  WHERE uptime = 0) AS a,
  (SELECT 
    id,
    stamp_date 
  FROM
    sample 
  WHERE uptime <> 0) AS b 
WHERE a.stamp_date < b.stamp_date 
AND a.id = b.id-1
GROUP BY a.id 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM