user7128548 user7128548 - 25 days ago 7
MySQL Question

MySQL; ORDER BY STR_TO_DATE doesn't work vice versa

This is my code:

SELECT id, title, date FROM table ORDER BY STR_TO_DATE(date,"%m/%d/%Y %h:%i:%s")


The date output is:

2016-12-11 14:40:00
2016-11-15 08:50:09
2016-11-15 08:54:58


SELECT id, title, date FROM table ORDER BY STR_TO_DATE(date,"%m/%d/%Y %h:%i:%s") DESC

doesn't work for me for some reason.

How can I reach to ORDER BY this?

2016-11-15 08:54:58
2016-11-15 08:50:09
2016-12-11 14:40:00


Edit:
date
is stored as
timestamp
in my MySQL database!

Answer

Assume you want the data that order by date ascending and time descending.

You can try this:

SELECT id, title, date 
FROM  table 
ORDER BY DATE(date) ASC , TIME(date) DESC