I have several values stored in my database as the DATETIME
datatype (YYYY-MM-DD HH:MM:SS), and I've been trying to get them in a descending order - Greatest to least (In the case of dates - Newest to oldest), though, oddly enough, it seems to ignore the existence of the DESC operator entirely.
The SQL query (Abbreviated):
SELECT post_datetime FROM post WHERE type=`published` ORDER BY post_datetime DESC LIMIT 3
And from that, they print in this order:
2014-04-30 11:55:11
2014-07-01 12:25:40
2014-07-02 12:28:03
(Those happen to be the "oldest" date entries in the database too)
Solution? I'll note that using DESC on other things (Such as normal numbers) doesn't work either. I've checked my syntax, tried quotes, no quotes, double quotes & such.
(Note - I won't be able to answer any further questions for several hours, though I'll do my best to respond once I return)
(6/11/17)
Edit: To clarify, for future readers, the syntax typo was not related to the issue and was an error on my part when typing an example. I had been using a homemade query builder for so long that I had forgotten the proper syntax at the time. The issue lied within my program's logic—not the query. The above example has been edited and is correct solution for anyone looking to perform the mentioned task. I'm sorry this has been viewed over 50k times... but now you know.
AND
between ORDER BY
and LIMIT
=
between ORDER BY
, LIMIT
keywords and conditionSo you query will look like:
SELECT post_datetime
FROM post
WHERE type = 'published'
ORDER BY post_datetime DESC
LIMIT 3
Try:
SELECT post_datetime
FROM post
WHERE type = 'published'
ORDER BY post_datetime DESC
LIMIT 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With