获取SQLite中满足本月要求的所有记录

时间:2017-03-15 15:21:19

标签: sql sqlite

我正在尝试获取本月发生的所有记录。

表格如下:

CREATE TABLE event (
    title TEXT NOT NULL,
    all_day INT NOT NULL,
    start INTEGER NOT NULL,
    end INTEGER NOT NULL,
    description TEXT NOT NULL,
    creator INTEGER NOT NULL,
    url TEXT NOT NULL,
    id TEXT NOT NULL,
    UNIQUE (ID)
);

我正在使用的查询是错误的:

select count(*) as event_count from
  (select title from event
  where (strftime('%m', datetime(end, 'unixepoch')) - 1) =
 (strftime('%m', 'now') + 0) group by title);

我不确定错误在哪里,感谢帮助。

由于

1 个答案:

答案 0 :(得分:1)

您不需要嵌套选择。

投影中应该有title

获得月份的简便方法是:strftime('%m',end,'unixepoch')

把它放在一起......

sqlite> CREATE TABLE event ( ...> title TEXT NOT NULL, ...> all_day INT NOT NULL, ...> start INTEGER NOT NULL, ...> end INTEGER NOT NULL, ...> description TEXT NOT NULL, ...> creator INTEGER NOT NULL, ...> url TEXT NOT NULL, ...> id TEXT NOT NULL, ...> UNIQUE (ID) ...> ); sqlite> insert into event values ("foo",0,0,5667567567,'',9,'',''); sqlite> select title, strftime('%m',end,'unixepoch') from event; foo|08 sqlite> select title, count(*) as eventcount from event where (strftime('%m','now') + 5) = (strftime('%m',end,'unixepoch') + 0) group by title; foo|1 sqlite>