mysql - 在 MySQL 中选择列为 0 的时间段之间的时间戳值

标签 mysql sql

我想选择正常运行时间为 0 或持续为 0 时的时间限制(stamp_date)。

表:

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 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');

示例表:

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) 链接在 06:20:00 到 06:30:00 之间断开 (uptime=0)

2) 链接在 07:10:00 到 08:00:00 之间断开 (uptime=0)

3) 链接在 08:20:00 到 08:30:00 之间断开 (uptime=0)

预期结果应如下所示:

+++++++++++++++++++++++++
| 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 |
+++++++++++++++++++++++++ 

有人可以帮我编写一个 SQL 或存储过程来实现上述结果吗?

谢谢

尤格什

最佳答案

如果您的 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;

这是它返回的数据,最后有一个额外的停机时间条目

+----------------------+ |当正常运行时间为 0 时 | +----------------------+ | 06:20:00 至 06:30:00 | | 07:10:00 至 08:00:00 | | 08:20:00 至 08:30:00 | | 08:40:00 到现在 | +----------------------+ 4 行一组(0.00 秒)

如果您的 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;

关于mysql - 在 MySQL 中选择列为 0 的时间段之间的时间戳值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27833286/

相关文章:

MySQL 检查 MAX 值是否重复

python - pandas to_sql 截断了我的数据

sql - PostgreSQL 多列唯一性不起作用

mysql - 如何创建触发器来查找一年的第一天?

mysql - 在考虑另一列的一列上选择 DISTINCT

sql - 带表达式的多列索引(PostgreSQL 和 Rails)

sql - 如何统计一个sql文件中有多少条sql语句?

java - JOOQ - 将结果转换为 Pojo

mysql - 内连接 2 计数语句

mysql - 为什么我的 MySQL 命令行无法在单个表上转储? (但我可以转储完整的数据库)