php选择某些日期之间和之间的所有预订

时间:2017-01-07 16:34:11

标签: php mysql date inner-join

我有一个查询,应该选择日期之间的所有预订

 SELECT t1.*, treatment.treatmentName, 
t2.userForename, t2.userSurname, 
t3.userForename as clientForename, t3.userSurname as clientSurname, t3.userPost as clientPost, t3.userAd1 as clientAd1,  
t4.userForename as createdForename, t4.userSurname as createdSurname 
            FROM booking t1 
            INNER JOIN treatment ON t1.idtreatment = treatment.idtreatment
            INNER JOIN user t2 ON t2.iduser = t1.idstylist
            INNER JOIN user t3 ON t3.iduser = t1.iduser
            INNER JOIN user t4 ON t4.iduser = t1.bookingCreatedBy
             WHERE 
             t1.bookingDate>=%s AND t1.bookingDate<=%s)  
             ORDER BY t1.bookingDate ASC, t1.bookingSTime ASC, t1.idtreatment ASC

如果我将查询的开始日期和结束日期传递给......

 SELECT t1.*, treatment.treatmentName, 
t2.userForename, t2.userSurname,
 t3.userForename as clientForename, t3.userSurname as clientSurname, t3.userPost as clientPost, t3.userAd1 as clientAd1, 
t4.userForename as createdForename, t4.userSurname as createdSurname 
 FROM booking t1 
INNER JOIN treatment ON t1.idtreatment = treatment.idtreatment 
INNER JOIN user t2 ON t2.iduser = t1.idstylist 
INNER JOIN user t3 ON t3.iduser = t1.iduser 
INNER JOIN user t4 ON t4.iduser = t1.bookingCreatedBy 
WHERE t1.bookingDate>='2017-01-01' AND t1.bookingDate<='2017-01-07' 
ORDER BY t1.bookingDate ASC, t1.bookingSTime ASC, t1.idtreatment ASC

如果我回显每行返回的日期,它似乎返回该范围之外的结果。例如,我进入我的结果集......

07/09 / 2016,07 / 09 / 2016,29 / 09 / 2016,17 / 09 / 2016,09 / 09 / 2016,05 / 01 / 2017,09 / 09 / 2016,209 / 09/09 ,07/09/2016

因为你可以看到实际上只有一个在我的数组中返回。

3 个答案:

答案 0 :(得分:1)

只是想一想,当你说日期错误时,你会显示什么日期栏?

确保它是t1.bookingDate

答案 1 :(得分:0)

评论太长了。您的“模板”查询看起来有:

        WHERE t1.bookingDate>=%s AND t1.bookingDate<=%s) 
-------------------------------------------------------^

结束语是语法错误。所以,你不应该得到任何结果。

您声称正在运行的查询是:

WHERE t1.bookingDate>='2017-01-01' AND t1.bookingDate<='2017-01-07' 

关闭的paren神秘地消失了。

对于此类问题,第一个假设是数据类型。但是,你说日期确实是一个日期。第二种可能性是WHERE中的更多条款。如果您有OR,那么您将获得意外结果。所以,将它完全括在括号中:

        WHERE (t1.bookingDate >= %s AND t1.bookingDate <= %s) 

答案 2 :(得分:-1)

您可以尝试替换 在哪里t1.bookingDate&gt; =%s AND t1.bookingDate&lt; =%s 到%s和%s之间的t1.bookingDate