I am having an SQL query problem. I am trying to get data in an ordered fashion between two dates. Here is a simplification of the code I am having problem with:
$query = "SELECT Name DateTime FROM table1";
$query .= " ORDER BY DateTime ASC";
$query .= " WHERE DateTime between date1 AND date2";
$result = mysql_query($query);
//fetch data here
while($row = mysql_fetch_array($result))
{
$output[] = $row;
}
print(json_encode($output));
The code above works when I comment out the line: $query .= ” ORDER BY DateTime ASC”;
What can I correct so that I can return ordered data between two dates?
EDIT:
When I move ORDER to after WHERE shouldn’t $output now be sorted? I return it to an android application and it does not seem to be the case.
The ORDER BY clause has to come at the end:
The general order for clauses is: