ORDER BY only day without time

8

In MySQL I have a column timestamp called dia_cadastr o (2016-06-21 11:27:32), in the query I want to give a ORDER BY only on the day, and disregard the time. That is, if they have products registered the same day, then it gives a ORDER BY RAND()

He had done so:

ORDER BY produto.dia_cadastro DESC, RAND()

But then he would have considered the time as well.

    
asked by anonymous 24.06.2016 / 15:07

1 answer

14

Just use date() :

ORDER BY date(produto.dia_cadastro) DESC, RAND()

The function takes only the part of the date without the time.

Just curious, if you just want the day of the month (weird) there use day() :

ORDER BY day(produto.dia_cadastro) DESC, RAND()

It is rare to have to order only by day, perhaps to generate statistics of days of greater movement.

    
24.06.2016 / 15:10