-1

我正在尝试获取所有 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";

我的第二种方法是(根据where 子句中的 SQL DateDifference):

$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";

它们都不起作用,那么如何从表中选择“date_return”之间的 day_difference 和now()小于 4 天?

编辑:

改变了

AND daydif < 4 

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

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

4

3 回答 3

0

使用DATEDIFF

WHERE DATEDIFF(date_return, now()) < 4
于 2013-02-20T17:36:36.860 回答
0

尝试:

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

having dayDiff < 4
于 2013-02-20T17:35:18.997 回答
0

尝试这个:

select * 
from  table
where DATEDIFF(day, present, future) < 4
于 2013-02-20T17:35:20.370 回答