从表中选择日差小于某个数字

时间:2013-02-20 17:32:01

标签: php sql

我正在尝试获取所有SQL条目。当前日期和到期日期之间的日差异是<超过4天

我的第一个方法是:

$sql_i_requested = "SELECT *, (To_days(date_return)-TO_DAYS(NOW())) as daydif FROM ".$tbl_name." 
WHERE (status!='completed' AND status!='canceled') 
AND owner_id=".$owner_id." 
AND daydif < 4 
ORDER BY date_created DESC";

我的第二个方法是(根据SQL DateDifference in a where clause):

$sql_i_requested = "SELECT * FROM ".$tbl_name." 
WHERE (status!='completed' AND status!='canceled') 
AND owner_id=".$owner_id." 
AND date_return > DateAdd(day, -3, getdate())
ORDER BY date_created DESC";

它们都不起作用,那么如何选择FROM table WHERE day_difference在“date_return”和now()之间的时间少于4天?

修改

改变

AND daydif < 4 

AND (To_days(date_return)-TO_DAYS(NOW())) < 4 

现在它正在运作。无论如何,也许你们可以提出其他解决方案。

3 个答案:

答案 0 :(得分:0)

尝试:

SELECT DATEDIFF(date_return, NOW()) AS dayDiff;

having dayDiff < 4

答案 1 :(得分:0)

试试这个:

select * 
from  table
where DATEDIFF(day, present, future) < 4

答案 2 :(得分:0)

使用DATEDIFF

WHERE DATEDIFF(date_return, now()) < 4