SQL 合并两个时间戳不同的表作为索引的查询
SQL Query to merge two tables with different timestamp as index
我不确定我的问题是否适用于 sql 查询。
我有以下两个 Mysql tables:
Table一个:
table 每 20 秒获取一个新值
a.Time a.Value
2015-05-14 07:36:36 49
2015-05-14 07:36:56 49
2015-05-14 07:37:16 49
2015-05-14 07:37:36 49
2015-05-14 07:37:56 50
2015-05-14 07:38:16 50
2015-05-14 07:38:36 50
2015-05-14 07:38:56 49
2015-05-14 07:39:16 49
2015-05-14 07:39:36 49
2015-05-14 07:39:56 49
2015-05-14 07:40:16 50
2015-05-14 07:40:36 50
2015-05-14 07:40:56 50
2015-05-14 07:41:16 49
2015-05-14 07:41:36 49
2015-05-14 07:41:56 50
2015-05-14 07:42:16 50
2015-05-14 07:42:36 49
2015-05-14 07:42:56 49
2015-05-14 07:43:16 49
2015-05-14 07:43:36 49
2015-05-14 07:43:56 49
2015-05-14 07:44:06 49
Tableb
table 获取值的每个变化一条新记录
b.Time b.Value
2015-05-14 07:34:50 1500
2015-05-14 07:38:52 2000
2015-05-14 07:40:52 0
2015-05-14 07:42:31 1500
结果应该如下所示
a.Time a.Value b.Time b.Value
2015-05-14 07:36:36 49 2015-05-14 07:34:50 1500
2015-05-14 07:36:56 49 2015-05-14 07:34:50 1500
2015-05-14 07:37:16 49 2015-05-14 07:34:50 1500
2015-05-14 07:37:36 49 2015-05-14 07:34:50 1500
2015-05-14 07:37:56 50 2015-05-14 07:34:50 1500
2015-05-14 07:38:16 50 2015-05-14 07:34:50 1500
2015-05-14 07:38:36 50 2015-05-14 07:34:50 1500
2015-05-14 07:38:56 49 2015-05-14 07:38:52 2000
2015-05-14 07:39:16 49 2015-05-14 07:38:52 2000
2015-05-14 07:39:36 49 2015-05-14 07:38:52 2000
2015-05-14 07:39:56 49 2015-05-14 07:38:52 2000
2015-05-14 07:40:16 50 2015-05-14 07:38:52 2000
2015-05-14 07:40:36 50 2015-05-14 07:38:52 2000
2015-05-14 07:40:56 50 2015-05-14 07:40:52 0
2015-05-14 07:41:16 49 2015-05-14 07:40:52 0
2015-05-14 07:41:36 49 2015-05-14 07:40:52 0
2015-05-14 07:41:56 50 2015-05-14 07:40:52 0
2015-05-14 07:42:16 50 2015-05-14 07:40:52 0
2015-05-14 07:42:36 49 2015-05-14 07:42:31 1500
2015-05-14 07:42:56 49 2015-05-14 07:42:31 1500
2015-05-14 07:43:16 49 2015-05-14 07:42:31 1500
2015-05-14 07:43:36 49 2015-05-14 07:42:31 1500
2015-05-14 07:43:56 49 2015-05-14 07:42:31 1500
2015-05-14 07:44:06 49 2015-05-14 07:42:31 1500
tablea 中的时间戳应该与 tableb 中的下一个更小的时间戳!
知道查询应该是什么样子吗?
非常感谢!
使用按 TIME
和 LIMIT 1
排序的子查询从 B 获取一条时间小于当前 A.Time:
的顶级记录
SELECT
a.Time,
a.Value,
(SELECT Time FROM B WHERE B.Time<=A.Time ORDER BY B.Time DESC LIMIT 1)
as B_Time,
(SELECT Value FROM B WHERE B.Time<=A.Time ORDER BY B.Time DESC LIMIT 1)
as B_Value
FROM A
ORDER BY A.Time
我不确定我的问题是否适用于 sql 查询。
我有以下两个 Mysql tables:
Table一个: table 每 20 秒获取一个新值
a.Time a.Value
2015-05-14 07:36:36 49
2015-05-14 07:36:56 49
2015-05-14 07:37:16 49
2015-05-14 07:37:36 49
2015-05-14 07:37:56 50
2015-05-14 07:38:16 50
2015-05-14 07:38:36 50
2015-05-14 07:38:56 49
2015-05-14 07:39:16 49
2015-05-14 07:39:36 49
2015-05-14 07:39:56 49
2015-05-14 07:40:16 50
2015-05-14 07:40:36 50
2015-05-14 07:40:56 50
2015-05-14 07:41:16 49
2015-05-14 07:41:36 49
2015-05-14 07:41:56 50
2015-05-14 07:42:16 50
2015-05-14 07:42:36 49
2015-05-14 07:42:56 49
2015-05-14 07:43:16 49
2015-05-14 07:43:36 49
2015-05-14 07:43:56 49
2015-05-14 07:44:06 49
Tableb table 获取值的每个变化一条新记录
b.Time b.Value
2015-05-14 07:34:50 1500
2015-05-14 07:38:52 2000
2015-05-14 07:40:52 0
2015-05-14 07:42:31 1500
结果应该如下所示
a.Time a.Value b.Time b.Value
2015-05-14 07:36:36 49 2015-05-14 07:34:50 1500
2015-05-14 07:36:56 49 2015-05-14 07:34:50 1500
2015-05-14 07:37:16 49 2015-05-14 07:34:50 1500
2015-05-14 07:37:36 49 2015-05-14 07:34:50 1500
2015-05-14 07:37:56 50 2015-05-14 07:34:50 1500
2015-05-14 07:38:16 50 2015-05-14 07:34:50 1500
2015-05-14 07:38:36 50 2015-05-14 07:34:50 1500
2015-05-14 07:38:56 49 2015-05-14 07:38:52 2000
2015-05-14 07:39:16 49 2015-05-14 07:38:52 2000
2015-05-14 07:39:36 49 2015-05-14 07:38:52 2000
2015-05-14 07:39:56 49 2015-05-14 07:38:52 2000
2015-05-14 07:40:16 50 2015-05-14 07:38:52 2000
2015-05-14 07:40:36 50 2015-05-14 07:38:52 2000
2015-05-14 07:40:56 50 2015-05-14 07:40:52 0
2015-05-14 07:41:16 49 2015-05-14 07:40:52 0
2015-05-14 07:41:36 49 2015-05-14 07:40:52 0
2015-05-14 07:41:56 50 2015-05-14 07:40:52 0
2015-05-14 07:42:16 50 2015-05-14 07:40:52 0
2015-05-14 07:42:36 49 2015-05-14 07:42:31 1500
2015-05-14 07:42:56 49 2015-05-14 07:42:31 1500
2015-05-14 07:43:16 49 2015-05-14 07:42:31 1500
2015-05-14 07:43:36 49 2015-05-14 07:42:31 1500
2015-05-14 07:43:56 49 2015-05-14 07:42:31 1500
2015-05-14 07:44:06 49 2015-05-14 07:42:31 1500
tablea 中的时间戳应该与 tableb 中的下一个更小的时间戳!
知道查询应该是什么样子吗?
非常感谢!
使用按 TIME
和 LIMIT 1
排序的子查询从 B 获取一条时间小于当前 A.Time:
SELECT
a.Time,
a.Value,
(SELECT Time FROM B WHERE B.Time<=A.Time ORDER BY B.Time DESC LIMIT 1)
as B_Time,
(SELECT Value FROM B WHERE B.Time<=A.Time ORDER BY B.Time DESC LIMIT 1)
as B_Value
FROM A
ORDER BY A.Time