如何在 SQLite 中使用特定长度来比较时间?

发布于 2024-10-26 22:28:58 字数 1057 浏览 1 评论 0原文

我以 YYYY-MM-DD HH:MM:SS 的形式存储事件时间。

我想要做的是找到在前一个触发器之后 7 小时以上发生的所有事件实例。在阅读 SQLite Date 文档后,我不确定执行此操作的最简单方法。我想我将不得不以某种方式使用 strftime,但我真的不知道如何处理它。

这是我必须构建的基本查询:

SELECT * FROM Event WHERE Event.DateTime (超过 7 小时后) (SELECT Trigger.DateTime FROM Trigger WHERE Event.DateTime > Trigger.DateTime)

我当前的测试数据设置如下:

DROP TABLE IF EXISTS "Event";
CREATE TABLE "Event" ("DateTime" TEXT, "ID" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE );
INSERT INTO "Event" VALUES('2000-01-01 08:00:00',1);
INSERT INTO "Event" VALUES('2000-01-01 09:00:00',2);
INSERT INTO "Event" VALUES('2000-01-01 14:00:00',3);
INSERT INTO "Event" VALUES('2000-01-01 15:00:00',4);
INSERT INTO "Event" VALUES('2000-01-01 20:00:00',5);
DROP TABLE IF EXISTS "Trigger";
CREATE TABLE "Trigger" ("DateTime" TEXT, "ID" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE );
INSERT INTO "Trigger" VALUES('2000-01-01 00:00:01',1);
INSERT INTO "Trigger" VALUES('2000-01-01 10:00:01',2);

因此它应该从事件中返回 1、2 和 5,因为这些是触发后 7 小时以上的事件。

I have event times stored in the form YYYY-MM-DD HH:MM:SS.

What I am trying to do is find all instances of Event where it occurs more than 7 hours after the preceding Trigger. I am not sure the easiest way to do this after reading the SQLite Date documentation. I guess I will have to use strftime somehow, but I'm really not sure how to approach it.

This is the basic query I'd have to build off of:

SELECT * FROM Event WHERE Event.DateTime (is more than 7 hours after) (SELECT Trigger.DateTime FROM Trigger WHERE Event.DateTime > Trigger.DateTime)

I have the test data currently set up like so:

DROP TABLE IF EXISTS "Event";
CREATE TABLE "Event" ("DateTime" TEXT, "ID" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE );
INSERT INTO "Event" VALUES('2000-01-01 08:00:00',1);
INSERT INTO "Event" VALUES('2000-01-01 09:00:00',2);
INSERT INTO "Event" VALUES('2000-01-01 14:00:00',3);
INSERT INTO "Event" VALUES('2000-01-01 15:00:00',4);
INSERT INTO "Event" VALUES('2000-01-01 20:00:00',5);
DROP TABLE IF EXISTS "Trigger";
CREATE TABLE "Trigger" ("DateTime" TEXT, "ID" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE );
INSERT INTO "Trigger" VALUES('2000-01-01 00:00:01',1);
INSERT INTO "Trigger" VALUES('2000-01-01 10:00:01',2);

so it should return 1, 2 and 5 from the events since those are the ones that are over 7 hours after a trigger.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-11-02 22:28:58

您是否有理由要针对 Trigger.DateTime 测试 Event.DateTime 两次?我认为这应该也有效:

SELECT * FROM Event 
WHERE EXISTS(SELECT 1 FROM Trigger 
             WHERE datetime(Event.DateTime,'+7 hours') > Trigger.DateTime)

UPDATE

因为您只想与最新的触发器进行比较,所以它会更像这样:

SELECT * FROM Event 
WHERE EXISTS(SELECT MAX(Trigger.DateTime) newest FROM Trigger 
             HAVING datetime(Event.DateTime,'-7 hours') > newest)

Edit

SELECT * FROM Event 
WHERE NOT EXISTS(SELECT 1 FROM Trigger 
                 WHERE datetime(Event.DateTime,'-7 hours') <= Trigger.DateTime)

SELECT * FROM Event 
WHERE datetime(Event.DateTime,'-7 hours') > 
            (SELECT MAX(Trigger.DateTime) newest FROM Trigger)

Edit2

SELECT * FROM Event 
WHERE NOT EXISTS(SELECT 1 FROM Trigger 
                 WHERE Trigger.DateTime BETWEEN datetime(Event.DateTime,'-7 hours') AND Event.DateTime)

SELECT * FROM Event 
WHERE datetime(Event.DateTime,'-7 hours') > 
            (SELECT MAX(Trigger.DateTime) newest FROM Trigger
             WHERE Trigger.DateTime < Event.DateTime)

Is there a reason you want to test the Event.DateTime against the Trigger.DateTime twice? I think this should work just as well:

SELECT * FROM Event 
WHERE EXISTS(SELECT 1 FROM Trigger 
             WHERE datetime(Event.DateTime,'+7 hours') > Trigger.DateTime)

UPDATE

Since you only want to compare to the latest trigger, it would be something more like this:

SELECT * FROM Event 
WHERE EXISTS(SELECT MAX(Trigger.DateTime) newest FROM Trigger 
             HAVING datetime(Event.DateTime,'-7 hours') > newest)

Edit

SELECT * FROM Event 
WHERE NOT EXISTS(SELECT 1 FROM Trigger 
                 WHERE datetime(Event.DateTime,'-7 hours') <= Trigger.DateTime)

SELECT * FROM Event 
WHERE datetime(Event.DateTime,'-7 hours') > 
            (SELECT MAX(Trigger.DateTime) newest FROM Trigger)

Edit2

SELECT * FROM Event 
WHERE NOT EXISTS(SELECT 1 FROM Trigger 
                 WHERE Trigger.DateTime BETWEEN datetime(Event.DateTime,'-7 hours') AND Event.DateTime)

SELECT * FROM Event 
WHERE datetime(Event.DateTime,'-7 hours') > 
            (SELECT MAX(Trigger.DateTime) newest FROM Trigger
             WHERE Trigger.DateTime < Event.DateTime)
煞人兵器 2024-11-02 22:28:58

怎么样

SELECT * FROM Event 
WHERE datetime(Event.DateTime,'+7 hours') > (SELECT Trigger.DateTime FROM Trigger 
                                           WHERE Event.DateTime > Trigger.DateTime)

How about

SELECT * FROM Event 
WHERE datetime(Event.DateTime,'+7 hours') > (SELECT Trigger.DateTime FROM Trigger 
                                           WHERE Event.DateTime > Trigger.DateTime)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文