0

我正在尝试编写一个查询,以获取在一组 30 秒长的时间之间发生的事件。

例子:

如果我使用这个查询:

SELECT videotime FROM codes
    WHERE participant = 2 AND (retrospective ='y' AND forks > 0)

输出:

00:16:30.000
00:21:00.000
00:21:30.000
00:27:00.000
00:38:30.000

然后我想在 00:16:31、00:16:32 以及 00:21:01、00:27:29.999 等从另一个表获取事件。

到目前为止我写的内容只匹配第一行(00:16:30),但我希望能够使其匹配更多。

SELECT id, videotime FROM commands WHERE participant = 2 AND
videotime >= (SELECT videotime FROM codes
    WHERE participant = 2 AND (retrospective ='y' AND forks > 0) ) AND
videotime <= time((SELECT videotime FROM codes
    WHERE participant = 2 AND (retrospective ='y' AND forks > 0) ), '+30 seconds');

上面的输出不正确:

28          00:16:31.021
29          00:16:31.635
30          00:16:34.948
31          00:16:35.004
32          00:16:37.101
33          00:16:37.144
34          00:16:37.992
35          00:16:38.029
36          00:16:42.740
37          00:16:42.910
38          00:16:43.724
39          00:16:49.756
40          00:16:49.836
41          00:16:58.788
42          00:16:58.834
43          00:16:59.936

我需要做什么来解决这个问题?

4

1 回答 1

1

我对 sqlite 没有任何经验,所以我的答案很可能在语法上是不正确的,但希望基本点很清楚:

SELECT id, videotime
FROM commands
WHERE participant = 2
      AND EXISTS
      ( SELECT time
        FROM (SELECT videotime 
              FROM codes
              WHERE participant = 2 
                    AND (retrospective ='y' AND forks > 0)) AS times
        WHERE commands.videotime - times.videotime >= 0
              AND commands.videotime - times.videotime <= 30
      )

(尤其是以秒为单位的时间差异的语法可能与我在这里写的不同,但我希望你/有人可以相应地调整它)

于 2013-08-21T20:33:01.653 回答