Select MySQL 数据库中的时间线
Select timeline in MySQL database
这是我的 MySQL table.
+-----------+-------------+---------------------+
| element | status | hour |
+-----------+-------------+---------------------+
| 18 | Available | 2020-01-19 14:23:49 |
| 18 | Unavailable | 2019-09-13 18:19:47 |
| 18 | Available | 2019-09-13 18:18:49 |
| 18 | Unavailable | 2019-09-09 08:22:45 |
| 19 | Available | 2019-09-07 19:13:56 |
| 19 | Available | 2019-09-03 18:13:49 |
+-----------+-------------+---------------------+
通常此 MySQL table 中每个元素 status
的行时间轴不可用/可用。
但碰巧对于元素编号 19,status
中行的时间线可用/可用:
+----------+-------------+---------------------+
| element | status | hour |
+----------+-------------+---------------------+
| 19 | Available | 2019-09-07 19:13:56 |
| 19 | Available | 2019-09-03 18:13:49 |
+----------+-------------+---------------------+
这是异常的意思吗
我需要拦截这些情况,即当时间线可用时每个元素的所有行 status
。
如何解决这个问题?
你能帮帮我吗?
#编辑01
+-----------+-------------+---------------------+---------+
| element | status | hour | ID |
+-----------+-------------+---------------------+---------+
| 18 | Available | 2020-01-19 14:23:49 | 6 |
| 18 | Unavailable | 2019-09-13 18:19:47 | 5 |
| 18 | Available | 2019-09-13 18:18:49 | 4 |
| 18 | Unavailable | 2019-09-09 08:22:45 | 3 |
| 19 | Available | 2019-09-07 19:13:56 | 2 |
| 19 | Available | 2019-09-03 18:13:49 | 1 |
+-----------+-------------+---------------------+---------+
根据您最初发布的示例数据,您可以使用子查询查看下一个和上一个状态,然后进行测试
select s.element,s.hour,s.`status`
from
(
select t.*,
(select concat(t1.status,',',t1.hour) from t t1
where t1.element = t.element and t1.hour < t.hour
order by t1.element,t1.hour desc limit 1) prev,
(select concat(t1.status,',',t1.hour) from t t1
where t1.element = t.element and t1.hour > t.hour
order by t1.element,t1.hour limit 1) nxt
from t
) s
where s.status = substring_index(s.nxt,',',1) or
s.status = substring_index(s.prev,',',1)
;
这是我的 MySQL table.
+-----------+-------------+---------------------+
| element | status | hour |
+-----------+-------------+---------------------+
| 18 | Available | 2020-01-19 14:23:49 |
| 18 | Unavailable | 2019-09-13 18:19:47 |
| 18 | Available | 2019-09-13 18:18:49 |
| 18 | Unavailable | 2019-09-09 08:22:45 |
| 19 | Available | 2019-09-07 19:13:56 |
| 19 | Available | 2019-09-03 18:13:49 |
+-----------+-------------+---------------------+
通常此 MySQL table 中每个元素 status
的行时间轴不可用/可用。
但碰巧对于元素编号 19,status
中行的时间线可用/可用:
+----------+-------------+---------------------+
| element | status | hour |
+----------+-------------+---------------------+
| 19 | Available | 2019-09-07 19:13:56 |
| 19 | Available | 2019-09-03 18:13:49 |
+----------+-------------+---------------------+
这是异常的意思吗
我需要拦截这些情况,即当时间线可用时每个元素的所有行 status
。
如何解决这个问题?
你能帮帮我吗?
#编辑01
+-----------+-------------+---------------------+---------+
| element | status | hour | ID |
+-----------+-------------+---------------------+---------+
| 18 | Available | 2020-01-19 14:23:49 | 6 |
| 18 | Unavailable | 2019-09-13 18:19:47 | 5 |
| 18 | Available | 2019-09-13 18:18:49 | 4 |
| 18 | Unavailable | 2019-09-09 08:22:45 | 3 |
| 19 | Available | 2019-09-07 19:13:56 | 2 |
| 19 | Available | 2019-09-03 18:13:49 | 1 |
+-----------+-------------+---------------------+---------+
根据您最初发布的示例数据,您可以使用子查询查看下一个和上一个状态,然后进行测试
select s.element,s.hour,s.`status`
from
(
select t.*,
(select concat(t1.status,',',t1.hour) from t t1
where t1.element = t.element and t1.hour < t.hour
order by t1.element,t1.hour desc limit 1) prev,
(select concat(t1.status,',',t1.hour) from t t1
where t1.element = t.element and t1.hour > t.hour
order by t1.element,t1.hour limit 1) nxt
from t
) s
where s.status = substring_index(s.nxt,',',1) or
s.status = substring_index(s.prev,',',1)
;