我正在研究JSON API,其中一个输入参数是指定一个如下定义的整数输入:
1 - would return results within one month from today
2 - would return results within three months from today
3 - would return results within six months from today
4 - would return results within one year from today
因此,本质上API将像这样使用:
http://url.com/index.php?route=api/order/all&search_time=1,将在今天一个月内从表格X返回所有结果。
我的表格中有datetime
字段,因此日期问题不是问题。我知道你可以在MONTH(datetime_item)
找回月份的MySQL函数在哪里,所以你可以像WHERE MONTH(datetime_item) = 8
一样使用它,但这似乎与我的使用无关。
所以在MySQL伪代码中,查询看起来像这样:
WHERE datetime is within three months ago from today
答案 0 :(得分:1)
试试这个,
SELECT * from table_name where datetime_item >= now()-interval 'x' month
日期算术也可以使用INTERVAL和+或 - 运算符执行:
date + INTERVAL expr unit
date - INTERVAL expr unit
Reference - documentation for date/time functionsDocumentation for date/time functions