今天和更早的日期之间的SQL

时间:2018-03-05 08:26:46

标签: php sql

我正在为我正在处理的项目进行查询。 我必须提出一个查询,向我显示今天的日期和将来的日期。目前我的查询是这样的:

$query = "SELECT * FROM systeem 
RIGHT JOIN vestiging ON vestiging.id = systeem.vestigingID 
LEFT JOIN systeemMeldkamer ON systeemMeldkamer.systeemID = systeem.id 
LEFT JOIN meldkamers ON meldkamers.id = systeemMeldkamer.meldkamerID 
LEFT JOIN systeemContract ON systeemContract.systeemID = systeem.id 
LEFT JOIN onderhoudsLog ON onderhoudsLog.systeemContractID = systeemContract.id  
LEFT JOIN contracten ON contracten.ident = systeemContract.contractIDENT
WHERE onderhoudsLog.onderhoudsDatum  LIKE '2017%'  
ORDER BY onderhoudsLog.onderhoudsDatum ASC";

此查询将为我提供所有类似" 2017%"的日期。 相反,我希望得到今天的日期以及今天之后的每个日期:

今天是:2018-03-05

我想得到的结果:2018-03-05,2018-03-06,2018-03-07

明天它是:2018-03-06然后它必须搜索2018-03-06之后的所有日期

我希望任何人都可以帮助我查询!不要介意" LEFT JOINS",这是因为我必须通过6个表格,以便我可以将维护日期与我们的客户的数字相匹配。

3 个答案:

答案 0 :(得分:3)

对于MySQL,您可以使用以下查询。添加where子句,如:WHERE onderhoudsLog.onderhoudsDatum >= CURDATE()

假设onderhoudsDatum字段是datetime数据类型

$query = "SELECT * FROM systeem 
RIGHT JOIN vestiging ON vestiging.id = systeem.vestigingID 
LEFT JOIN systeemMeldkamer ON systeemMeldkamer.systeemID = systeem.id 
LEFT JOIN meldkamers ON meldkamers.id = systeemMeldkamer.meldkamerID 
LEFT JOIN systeemContract ON systeemContract.systeemID = systeem.id 
LEFT JOIN onderhoudsLog ON onderhoudsLog.systeemContractID = systeemContract.id  
LEFT JOIN contracten ON contracten.ident = systeemContract.contractIDENT
WHERE onderhoudsLog.onderhoudsDatum >= CURDATE()  
ORDER BY onderhoudsLog.onderhoudsDatum ASC";

答案 1 :(得分:0)

你可以试试这段代码

$today = date('Y-m-d');

$query = "SELECT * FROM systeem 
  RIGHT JOIN vestiging ON vestiging.id = systeem.vestigingID 
  LEFT JOIN systeemMeldkamer ON systeemMeldkamer.systeemID = systeem.id 
  LEFT JOIN meldkamers ON meldkamers.id = systeemMeldkamer.meldkamerID 
  LEFT JOIN systeemContract ON systeemContract.systeemID = systeem.id 
  LEFT JOIN onderhoudsLog ON onderhoudsLog.systeemContractID = systeemContract.id  
  LEFT JOIN contracten ON contracten.ident = systeemContract.contractIDENT
  WHERE onderhoudsLog.onderhoudsDatum  => $today  
  ORDER BY onderhoudsLog.onderhoudsDatum ASC";

您使用PHP的date()函数获取日期,将其格式化为MySql日期字符串,然后在查询中使用该日期:

WHERE onderhoudsLog.onderhoudsDatum  => $today

答案 2 :(得分:0)

是onderhoudsLog.onderhoudsDatum日期字段,然后使用'> = CURRENT_DATE'而不是'喜欢“2017%”'并按照onderhoudsLog.onderhoudsDatum订购