Synyster Synyster - 2 months ago 6
MySQL Question

MySQL Order rows by date

I'm trying to order my rows by the date that each row has.

One row has

2016-09-15 15:36
and one has
2016-08-15 13:12
How would i order them so that the highest one is at top?

Their row is called
th_activity
and i know that in a normal query, you would do something like
SELECT * FROM threads ORDER BY th_activity DESC
but i believe i know the problem. I use varchar as the type for the row. I'm not really sure what the length/value should be for date if that's what i must use.

If someone can explain how i would order this properly, i'd appreciate it.

Answer

You could have set the type of column as timstamp instead of varchar, which doesn't care about the length of data (only that while inserting data take care about the format).

Check this question to ordery by timestamp:

MYSQL - Order timestamp values ascending in order, from newest to oldest?

SELECT th_activity
FROM tableName
ORDER BY timestamp desc;
Comments