2010-02-22 58 views
36

我有一个sqlite表与出生日期。我想执行一个查询,选择那些记录,年龄超过30 我曾尝试以下,但它不工作:sqlite select与日期条件

select * from mytable where dob > '1/Jan/1980' 
select * from mytable where dob > '1980-01-01' 

回答

56

一些像这样的事情,可以使用:

select dateofbirth from customer Where DateofBirth BETWEEN date('1004-01-01') AND date('1980-12-31'); 
select dateofbirth from customer where date(dateofbirth)>date('1980-12-01'); 
select * from customer where date(dateofbirth) < date('now','-30 years'); 

如果您正在使用SQLite V3.7.12或更高

不使用date(dateofbirth)只使用dateofbirth。因此,您的查询应该是这样的:

select * from customer where dateofbirth < date('now','-30 years'); 
+18

所有从google来到这里的人:现在不再使用sqlite v3.7.12了;不要使用'date(dateofbirth)',只是'dateofbirth'会很好。 – wecing 2013-01-12 22:16:14

2

尝试使用的日期格式写“YYYY-MM- DD”

select * from mytable where dob > '1980-01-01' 

更programic的方法是这样的:

select * from mytable where datediff(dob, now()) > 30 

你需要找到sqlite的特定语法。

+0

SELECT * FROM MYTABLE其中DOB> '1980-01-01' 不会在2010年工作 – Thunder 2010-02-22 07:46:54

+2

返回'select * from mytable'dob>'1980-01-01''可能不起作用,但是目前这个命令在'sqlite3'上完美工作。 – Shane 2013-12-17 09:56:34

+1

我同意,在'sqlite3'中它运行良好。 – 2015-11-10 14:07:23

19

使用魔法文档在the sqlite website

select * from mytable where dob < date('now','-30 years');