Greetings Dear! There is such a SQL query:

$sql = "SELECT * FROM visit WHERE dateTime BETWEEN '$dateStart' AND '$dateEnd' ORDER BY dateTime DESC"; 

This request makes a selection by date range, date in format 2016-10-11 00:41:08
I try with DESC and ASC, but the result is the same - the lines are sorted in ascending order, i.e. first the oldest records, and it is necessary that the most recent records by date and time be displayed at the beginning. Help please with the request, I can not find the correct answer. Thank you for attention!

  • What is the dateTime field type? Is it a CHAR? - rjhdby
  • 2
    I just leave it here, stackoverflow.com/questions/9511882/… - Aleksei Chibisov
  • field type - datetime - Movie Trailers
  • So all the same, how can I make a request correctly? Help me please. In this topic, stackoverflow.com/questions/9511882/sorting-by-date-time-in-descending-order, I did not understand anything. - Movie Trailers
  • one
    @ MovieTrailers, and, so you have mysql - then you need SELECT * FROM visit WHERE `dateTime` BETWEEN '$ dateStart' AND '$ dateEnd' ORDER BY` dateTime` DESC - Ksenia

1 answer 1

The problem is solved by escaping the dateTime field - as in mysql it is a reserved word:

 SELECT * FROM visit WHERE `dateTime` BETWEEN '$dateStart' AND '$dateEnd' ORDER BY `dateTime` DESC;