4

I need to get the sorted dates by query in SQLite that are in timestamp format

I tried this query but not works well

SELECT  * FROM  allmessages WHERE  TIMESTAMP <=  DATE('now','-365 day') AND  TIMESTAMP >=  DATE('now')  order by  TIMESTAMP  DESC

I need sorted dates like

From today to before 1 year

From today to before 30 days

From today to before 7 days

These are the dates that are in DB

enter image description here

Sory for bad english...

4

3 回答 3

3

这是我所拥有的:

SELECT *
FROM allmessages
WHERE timestamp <= strftime('%s', 'now')*1000
AND timestamp >= strftime('%s', 'now', '-365 days')*1000;

一些警告:

自纪元以来,您的时间戳似乎以毫秒为单位?我将 SQLite 的内部时间戳乘以 1000 以反映这一点。

您想要今天和一年前(2012 年)之间的日期吗?如果是这样,上面的查询就可以了;如果您想要从今天到一年后的日期(2014 年),请翻转比较运算符。

我正在使用 SQLite 的内部rowid列;您可以通过将其替换为您自己的 id 列来将其移动到其他数据库。

这是一个现场 SQL Fiddle 演示。

于 2013-08-26T07:21:21.493 回答
1

不应该只是

SELECT  * FROM  allmessages WHERE  DATE(TIMESTAMP) >=  DATE('now','-365 day') AND   DATE(TIMESTAMP) <  DATE('now')  order by  TIMESTAMP  DESC
于 2013-08-22T13:18:40.623 回答
1

尝试这个:

SELECT  * FROM  allmessages WHERE  DATE(TIMESTAMP/1000, 'unixepoch') >=  DATE('now','-365 day') AND   DATE(TIMESTAMP/1000, 'unixepoch') <  DATE('now')  order by  TIMESTAMP  DESC

有关详细信息,请参见那里(查找时间戳):http ://www.sqlite.org/lang_datefunc.html

于 2013-08-26T06:52:14.437 回答