简体   繁体   English

FMDB如何在Sqlite iPhone dev上查询日期范围内的数据

[英]FMDB how can I query data from date range on Sqlite iPhone dev

I am trying to perform a query on a table where there is a date field and I want to query over a range of values The where clause looks like the following: 我试图在有一个日期字段的表上执行查询,我想查询一系列值where子句如下所示:

[db executeQuery:@"select * from test where myDate BETWEEN date('now','-7 days') and date('now')"];

But it doesn't seem to work :( 但它似乎不起作用:(

If you used DATETIME column with table declaration, you can do this: 如果您使用带有表声明的DATETIME列,则可以执行以下操作:

[db executeQuery:@"SELECT * FROM test WHERE myDate BETWEEN ? and ?", [[NSDate dateWithTimeInterval:-86400*7 sinceDate:[NSDate date]], [NSDate date]];

Note that -86400 is seconds of a day, negative indicates "days before" like your question. 请注意, -86400是一天中的秒数,负数表示“前几天”,就像您的问题一样。

Also note that BETWEEN syntax includes the very second of the date you set. 另请注意, BETWEEN语法包含您设置日期的第二个。 Use WHERE myDate > ? and myDate < ? 使用WHERE myDate > ? and myDate < ? WHERE myDate > ? and myDate < ? if you need non-inclusive match. 如果你需要非包容性的比赛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM