SQL查询条件与日期

时间:2013-02-23 14:01:02

标签: sql

所以,我在这里有这个问题:

$strSQL = "SELECT formas.*, SMS_SERVISI.IDTICKET, SMS_SERVISI.MBYLLUR,SMS_SERVISI.time_added,servis_furnitor.id_servis,servis_furnitor.furnitori,servis_furnitor.kohezgjatja
FROM formas  
LEFT JOIN servis_furnitor ON formas.furnitori = servis_furnitor.id_servis 
LEFT JOIN SMS_SERVISI ON formas.ID = SMS_SERVISI.IDTICKET 
ORDER BY formas.id DESC 
WHERE $today-formas.data_fillim > servis_furnitor.kohezgjatja";

最后一行是错的,我知道,我的意思是我可以去那里.. 我有这个订单,他们的开始日期是formas.data_fillim,我有今天的日期:

$today = date("Ymd"); 

因此,$today-formas.data_fillim之间的差异不应大于servis_furnitor.kohezgjatja这是一个整数本身,它会显示天数

formas.data_fillim

是日期类型..

我需要提取所有数据,这些数据与今天的日期和开始日期的差异不大于“kohezgjatja”中预定义的天数

任何帮助请... 感谢

更新

$today = date("Y-m-d H:i:s", time());  


echo $strSQL = "SELECT  formas.*,
        SMS_SERVISI.IDTICKET,
        SMS_SERVISI.MBYLLUR,
        SMS_SERVISI.time_added,
        servis_furnitor.id_servis,
        servis_furnitor.furnitori,
        servis_furnitor.kohezgjatja
FROM formas
LEFT JOIN servis_furnitor
        ON formas.furnitori = servis_furnitor.id_servis
LEFT JOIN SMS_SERVISI
        ON formas.ID = SMS_SERVISI.IDTICKET
WHERE DATEDIFF ( day , '$today' , formas.data_fillim ) > servis_furnitor.kohezgjatja
ORDER BY formas.id DESC"

2 个答案:

答案 0 :(得分:0)

SELECT语句的正确格式为

SELECT ....
FROM...
WHERE ...
GROUP ....
ORDER BY...

所以在你的情况下,

SELECT  formas.*,
        SMS_SERVISI.IDTICKET,
        SMS_SERVISI.MBYLLUR,
        SMS_SERVISI.time_added,
        servis_furnitor.id_servis,
        servis_furnitor.furnitori,
        servis_furnitor.kohezgjatja
FROM formas
LEFT JOIN servis_furnitor
        ON formas.furnitori = servis_furnitor.id_servis
LEFT JOIN SMS_SERVISI
        ON formas.ID = SMS_SERVISI.IDTICKET
WHERE DATEDIFF ( day , $TODAY , formas.data_fillim )  > servis_furnitor.kohezgjatja
ORDER BY formas.id DESC

答案 1 :(得分:0)

这样的事情应该有效:

where formas.data_fillim <= DateAdd(day, servis_furnitor.kohezgjatja, getdate())

您可能需要在servis_furnitor.kohezgjatja前面加一个减号,具体取决于您在那里存放的东西。