MySQL:选择一个 table 的行具有相同的时间戳或最接近+小于另一个 table 中的时间戳

MySQL: selecting one table's row that either has same timestamp or closest+less than the timestamp in another table

我有 TableATable B,如下所示。目前,正如您在下面的查询中看到的,TableA 基于 box_id 加入 TableB 并且 TableB 中的所有行 timestamp 在 [的 1 分钟内=17=] timestamp

我想修改此查询,而不是 selecting TableBTableA 行 1 分钟内,我想 select TableB 具有等效 timestamp 或其 timestamp 最接近+小于 TableA 中的 timestamp 的行。

下面是 TableATableB 以及我想要的 Desired Table 的示例。一旦实现,我就不会按 rainfall, hail, weather 分组,因为没有必要。任何 idea/help 将不胜感激。

Table答:

   id     box_id        timestamp       
373001645   1       2020-05-07 06:00:20 
373001695   1       2020-05-07 06:02:26
373001762   1       2020-05-07 06:05:17
373001794   1       2020-05-07 06:06:38
373001810   2       2020-05-07 06:07:21

TableB:

   id     box_id        timestamp                  data
373001345   1       2020-05-07 06:00:20     {"R": 0.114, "H": 20.808}
373001395   1       2020-05-07 06:02:26     {"R": 0.12, "H": 15.544}
373001462   1       2020-05-07 06:03:01     {"R": 0.006, "H": 55.469}
373001494   1       2020-05-07 06:04:38     {"R": 0.004, "H": 51.85}
373001496   1       2020-05-07 06:05:18     {"R": 0.02, "H": 5.8965}
373001497   1       2020-05-07 06:06:39     {"R": 0.12, "H": 54.32}
373001510   2       2020-05-07 06:07:09     {"R": 0.34, "H": 1.32}
373001511   2       2020-05-07 06:07:29     {"R": 0.56, "H": 32.7}

想要Table:

   id           timestamp           rainfall     hail                weather
373001345   2020-05-07 06:00:20     0.114       20.808      {"Rainfall": 0.114, "Hail": 20.808}
373001395   2020-05-07 06:02:26     0.12        15.544      {"Rainfall": 0.12, "Hail": 15.544}
373001462   2020-05-07 06:05:17     0.004       51.85       {"Rainfall": 0.004, "Hail": 51.85}
373001494   2020-05-07 06:06:38     0.02        5.8965      {"Rainfall": 0.02, "Hail": 5.8965}
373001496   2020-05-07 06:07:21     0.34        1.32        {"Rainfall": 0.34, "Hail": 1.32}

查询:

SELECT tableA.id, tableA.timestamp AS timestamp,
                (tableB.data->'$.R') as rainfall, (tableB.data->'$.H') as hail,
                JSON_OBJECT("Hail", (tableB.data->'$.H'),"Rainfall", (tableB.data->'$.R')) AS weather
                FROM tableA tableA
                  LEFT JOIN tableB tableB ON tableA.box_id = tableB.box_id
                    AND TIMESTAMPDIFF(MINUTE, tableB.timestamp, tableA.timestamp) BETWEEN -1 AND 1
                WHERE
                  tableA.timestamp BETWEEN '2020-05-07 00:00:00' AND '2020-05-07 23:59:59'
                GROUP BY tableA.id,rainfall,hail,weather
                ORDER BY tableA.timestamp ASC;

您可以在 ON 条件中使用相关子查询来查找 tableB 中小于或等于 [=15= 中的当前行时间戳的最大值 timestamp ],JOIN 基于此:

SELECT A.id, 
       A.timestamp AS timestamp,
       B.data->'$.R' as rainfall,
       B.data->'$.H' as hail,
       JSON_OBJECT("Hail", B.data->'$.H',
                   "Rainfall", B.data->'$.R'
                  ) AS weather
FROM tableA A
LEFT JOIN tableB B ON B.box_id = A.box_id
                  AND B.timestamp = (SELECT MAX(timestamp)
                                     FROM tableB b2
                                     WHERE b2.box_id = A.box_id
                                       AND b2.timestamp <= A.timestamp)
ORDER BY A.timestamp

输出:

id          timestamp               rainfall    hail    weather
373001645   2020-05-07 06:00:20     0.114       20.808  {"Hail": 20.808, "Rainfall": 0.114}
373001695   2020-05-07 06:02:26     0.12        15.544  {"Hail": 15.544, "Rainfall": 0.12}
373001762   2020-05-07 06:05:17     0.004       51.85   {"Hail": 51.85, "Rainfall": 0.004}
373001794   2020-05-07 06:06:38     0.02        5.8965  {"Hail": 5.8965, "Rainfall": 0.02}
373001810   2020-05-07 06:07:21     0.34        1.32    {"Hail": 1.32, "Rainfall": 0.34}

Demo on dbfiddle