如何在 SQLite 中使用特定长度来比较时间?
我以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否有理由要针对 Trigger.DateTime 测试 Event.DateTime 两次?我认为这应该也有效:
UPDATE
因为您只想与最新的触发器进行比较,所以它会更像这样:
Edit
Edit2
Is there a reason you want to test the Event.DateTime against the Trigger.DateTime twice? I think this should work just as well:
UPDATE
Since you only want to compare to the latest trigger, it would be something more like this:
Edit
Edit2
怎么样
How about