SQL sqlite 选择日期条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2309227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sqlite select with condition on date
提问by Thunder
I have an sqlite table with Date of Birth. I would like to execute a query to select those records where the age is more than 30. I have tried the following but it doesn't work:
我有一个带有出生日期的 sqlite 表。我想执行一个查询来选择那些年龄超过 30 岁的记录。我尝试了以下但它不起作用:
select * from mytable where dob > '1/Jan/1980'
select * from mytable where dob > '1980-01-01'
回答by Thunder
Some thing like this could be used:
可以使用这样的东西:
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');
If you are using Sqlite V3.7.12 or greater
如果您使用的是 Sqlite V3.7.12 或更高版本
Dont use date(dateofbirth)
just use dateofbirth
. So your query would look like this:
不要使用date(dateofbirth)
只使用dateofbirth
. 所以你的查询看起来像这样:
select * from customer where dateofbirth < date('now','-30 years');
回答by My Other Me
Using the magic docs at the sqlite website:
使用sqlite 网站上的魔术文档:
select * from mytable where dob < date('now','-30 years');
回答by Yada
Try writing using the date format 'YYYY-MM-DD'
尝试使用日期格式“YYYY-MM-DD”
select * from mytable where dob > '1980-01-01'
A more programic way would be something like this:
更程序化的方式是这样的:
select * from mytable where datediff(dob, now()) > 30
You'll Need to find specific syntax for sqlite.
您需要找到 sqlite 的特定语法。