Sheriff Said Elahl Sheriff Said Elahl - 6 months ago 27
MySQL Question

How To Select in mysql from Starting date

How can I select range of dates between start Date to Maximum date available in the table (Ex. starting from 03-05-2016 to highest date available in the table).

I failed in

select x from y where date between a and b


as I have no value for b (end date).

I added the full code, I used >= but the statement has something has something wrong. It echo "Failed"

$StartDate = $_POST["StartDate"];
if($stmt = $mysqli->prepare("SELECT ActivityDate, ANCO, CoreSite, MailSubject, AssignedDCO, Notes FROM ActivityPlanner WHERE ActivityDate >= ?"))
{
$stmt->bind_param("s", $StartDate);
$stmt->execute();
$stmt->bind_result($ActivityDate, $ANCO, $CoreSite, $MailSubject, $AssignedDCO, $Notes);
while ($stmt->fetch())
{
$Str = strval($ANCO);
echo json_encode($Str).",".json_encode($CoreSite).",".json_encode($MailSubject).",".json_encode($AssignedDCO).",".json_encode($Notes).",".json_encode($ActivityDate).",";
}
$stmt->close();
echo ("Successfull");
}
else{
echo ("Failed");
$mysqli->close();
}

Answer

You can only use BETWEEN when you know the range. For your scenario, you can just simply use > or >=.

-- Note: Dates need to be in the format 'yyyy-mm-dd'
SELECT x FROM y WHERE date >= '2016-03-05'