ORDER BY only day without hour

Asked

Viewed 528 times

8

In Mysql I have a column timestamp calling for dia_cadastro (2016-06-21 11:27:32), in consultation I want to give a ORDER BY only on the day, and disregard the time. That is, if they have products registered on the same day, after it gives a ORDER BY RAND()

Had done so:

ORDER BY produto.dia_cadastro DESC, RAND()

But then he’d consider the time as well.

  • 1

    You’re making the filter by the month?

1 answer

14


Just use the function date():

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

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

Just as a curiosity, if you only want the day of the month (weird) use there day():

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

I put in the Github for future reference.

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

  • I would propose the DAY() (weird) only useful with a filter.

  • Thanks, it was that was!

  • I believe he meant that it’s odd to check only the day as described in the question, would ignore the months and would cause problems.

  • @Marcelodeandrade if this is it, explained then :) I do not know if it would cause problems, it depends on the problem, it is rare someone, in a data set, have to take the day and ignore the month, the year. Taking the day is more about doing a specific operation on a single data, ordering, and weird, but I see some case for this. It may be busier day statistics, for example.

  • That’s right, @bigown. As he said " just on the day, and disregarding the schedule, "it must have been the attempt to treat only the date of datetime

  • @Marcelodeandrade think this was the intention. The person exchanges nomenclature, q is even normal, but programming can bring wrong result :)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.