sqlite日期比较:任何想法为什么以下sqlite选择时间> date('now')不返回collect行?

时间:2011-07-07 16:29:03

标签: sqlite

tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db "select * from  users where timeCreate > date('now')"
1|test0@test.com|testuser|test_pass|0|2011-07-01 05:49:19.365472|2011-07-01 05:49:19.395194
2|test1@test.com|testuser1|test_pass_new|10|2011-07-01 05:49:19.376098|2011-07-01 05:49:19.394416
3|test_1@test.com|testuser_1|test_pass_1|2|2011-07-01 05:49:19.407550|2011-07-01 05:49:19.407550


tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db "SELECT datetime('now'); "                      2011-07-01 06:22:44

tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db "select * from  users where timeCreate < date('now')"

tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db "select * from  users where timeCreate < date('now', '-5 minutes')"

tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db ".dump"
BEGIN TRANSACTION;
CREATE TABLE users
                (user_id    INTEGER PRIMARY    KEY,
                 email      text,
                 userName   text,
                 password   text,
                 usertype   int,
                 timeCreate DATE,
                 lastLogin  DATE);
INSERT INTO "users" VALUES(1,'test0@test.com','testuser','test_pass',0,'2011-07-01 05:49:19.365472','2011-07-01 05:49:19.395194');
INSERT INTO "users" VALUES(2,'test1@test.com','testuser1','test_pass_new',10,'2011-07-01 05:49:19.376098','2011-07-01 05:49:19.394416');
....

只需尝试此查询,它仍无效:

tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db "select datetime(timeCreate) from  users "
2011-07-01 05:49:19
2011-07-01 05:49:19
tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db "select * from  users where datetime(timeCreate) < date('now', '-5 minutes')"
tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db "select * from  users where datetime(timeCreate) > date('now', '-5 minutes')"
1|test0@test.com|testuser|test_pass|0|2011-07-01 05:49:19.365472|2011-07-01 05:49:19.395194
2|test1@test.com|testuser1|test_pass_new|10|2011-07-01 05:49:19.376098|2011-07-01 05:49:19.394416
tlee@tlee-vm:~/work/hl$ sqlite3 sdb/testUsersSDB.db "SELECT datetime('now', '-5 minutes'); "
2011-07-01 08:24:06

我期待“&lt;”查询返回行而不是“&gt;”。

1 个答案:

答案 0 :(得分:2)

您只是将日期与日期时间进行比较,因为日期函数返回日期而非日期与时间:

sqlite> select date('now', '-5 minutes');
2011-07-07

所以只需使用日期时间功能:

sqlite> select datetime('now', '-5 minutes');
2011-07-07 17:23:29