sqlite选择条件日期

Thu*_*der 49 sql database sqlite date

我有一个带有出生日期的sqlite表.我想执行一个查询来选择那些年龄超过30岁的记录.我尝试过以下但是它不起作用:

select * from mytable where dob > '1/Jan/1980'
select * from mytable where dob > '1980-01-01'
Run Code Online (Sandbox Code Playgroud)

Thu*_*der 72

可以使用这样的东西:

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');
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Sqlite V3.7.12或更高版本

不要date(dateofbirth)只使用dateofbirth.所以你的查询看起来像这样:

select * from customer where dateofbirth < date('now','-30 years');
Run Code Online (Sandbox Code Playgroud)

  • 所有来自谷歌的人:现在它不再适用于sqlite v3.7.12; 不要使用`date(dateofbirth)`,只要`dateofbirth`就好了. (20认同)
  • 只需要注意一件事:```date(field)&gt; date('2018-05-20')```和```field&gt;'2018-05-20'```返回不同的结果我因此使用函数```date()```是它对我来说正确工作的唯一方法**如果该字段不是Date而是DateTime **。 (2认同)

My *_* Me 21

sqlite网站上使用魔术文档:

select * from mytable where dob < date('now','-30 years');
Run Code Online (Sandbox Code Playgroud)


Yad*_*ada 5

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

select * from mytable where dob > '1980-01-01'
Run Code Online (Sandbox Code Playgroud)

更程序化的方式是这样的:

select * from mytable where datediff(dob, now()) > 30
Run Code Online (Sandbox Code Playgroud)

您需要为 sqlite 找到特定的语法。

  • 早在 2010 年,`select * from mytable where dob &gt; '1980-01-01'` 可能不起作用,但目前这个命令在 `sqlite3` 上运行良好。 (4认同)