简体   繁体   English

从日期小于提供日期的数据库中选择

[英]Select from database where date is less than a supplied date

I'm trying to pull rows from a database where the date is within two weeks of the current date, I'm using CodeIgniter and this is my model: 我正在尝试从日期在当前日期的两周之内的数据库中提取行,我正在使用CodeIgniter,这是我的模型:

function GetToDoItems($userID)
{
  $range = date('Y-m-d', strtotime("+2 weeks"));
  $query = $this->db->query("SELECT * FROM FYP_Milestones
                            INNER JOIN FYP_Modules ON FYP_Milestones.ModuleID = FYP_Modules.ID
                            WHERE FYP_Milestones.MilestoneDue < $range
                            ORDER BY FYP_Milestones.MilestoneDue ASC
                            ");
  return $query->result();
}

It runs this query: 它运行以下查询:

SELECT * FROM FYP_Milestones INNER JOIN FYP_Modules ON FYP_Milestones.ModuleID = FYP_Modules.ID WHERE FYP_Milestones.MilestoneDue < 2016-04-14 ORDER BY FYP_Milestones.MilestoneDue ASC

I have a row in the database like so: 我在数据库中有这样一行: 在此处输入图片说明

Considering 2016-04-07 is less than 2016-04-14 by 7 days I'm expecting that row alone to be pulled, but SQL is returning an empty result , why is this the case? 考虑到2016-04-07比2016-04-14少了7天,我希望单独提取该行, 但是SQL返回空结果 ,为什么会这样呢?

您实际上不需要该范围的PHP,请使用SQL:

WHERE FYP_Milestones.MilestoneDue < (CURDATE() + INTERVAL 2 WEEK)   

Add single quotes to the date value like: 将单引号添加到日期值,例如:

$query = $this->db->query("SELECT * FROM FYP_Milestones
                        INNER JOIN FYP_Modules ON FYP_Milestones.ModuleID = FYP_Modules.ID
                        WHERE FYP_Milestones.MilestoneDue < '".$range."'
                        ORDER BY FYP_Milestones.MilestoneDue ASC
                        ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM