SQL - ORDER BY 'datetime' DESC

I have several values ​​stored in my database as the DATETIME data type ( YYYY-MM-DD HH: MM: SS ), and I tried to get them in descending order - The highest minimum (in the case of dates, from the newest to the most old), although, oddly enough, it seems to completely ignore the existence of the DESC operator.

SQL query (abbreviated):

 SELECT post_datetime FROM post WHERE type=`published` ORDER BY post_datetime DESC LIMIT 3 

And from this they are printed in the following order:

 2014-04-30 11:55:11 2014-07-01 12:25:40 2014-07-02 12:28:03 

(These are also the oldest date entries in the database)

Decision? I note that using DESC on other things (such as normal numbers) does not work either. I checked my syntax, looked at quotes, quotes, double quotes, etc.

(Note. I will not answer any additional questions for several hours, although I will do my best to answer as soon as I return)


(6/11/17)

Change To clarify, for future readers, a typo of syntax was not related to the problem and was a mistake on my part when entering the example. I used the home query designer for so long that I forgot about the correct syntax at the time. The problem depended on my program logic, not on the request. The above example has been edited and is the right solution for those who want to complete the specified task. I am sorry that it has been viewed more than 50 thousand times ... but now you know.

+6
source share
3 answers
  • use single quotes for strings
  • DO NOT put single quotes around table names (use instead)
  • DO NOT put single quotes around numbers (you can, but it's harder to read)
  • DO NOT put AND between ORDER BY and LIMIT
  • DO NOT put = between ORDER BY , LIMIT keywords and condition

So your query will look like this:

 SELECT post_datetime FROM post WHERE type = 'published' ORDER BY post_datetime DESC LIMIT 3 
+12
source

Try:

 SELECT post_datetime FROM post WHERE type = 'published' ORDER BY post_datetime DESC LIMIT 3 
+1
source

Clear the quotes here:

is an:

 ORDER BY = 'post_datetime DESC' AND LIMIT = '3' 

Must be:

 ORDER BY post_datetime DESC LIMIT 3 
+1
source

Source: https://habr.com/ru/post/971744/


All Articles