2
I have a table with the fields mes E ano
. How can I sort the results according to the month.
I’m already ordering according to the year
=> SELECT * FROM tabela WHERE id_cliente = '$id_cliente' ORDER BY ano DESC
Now how can I order the months too ?
Ex ( January, Feb, sea... )
PS: mes (varchar 255), ano (varchar 255) that would be a bad practice?
How are you storing the month? Number or written?
– Roberto de Campos
Written, that would be a bad practice?
– Mustache Web
Yes, for performance purposes, put a field of the kind
tinyint
and number 1 to 12, it would be lighter and simpler to do the ordering you want– Roberto de Campos
Bad practice for using the name? It will depend on the rule of your application. But what to use
varchar 255
for this is absurd, is.– Marcelo de Andrade
@Marcelodeandrade can tell me about these values? how to use them correctly?.
– Mustache Web
In general I would not consider ideal. The
VARCHAR 255
using utf8mb4 will cost 1020 bytes at most. In older versions of Mysql, I don’t know currently, it would even be necessary to enable theinnodb_large_prefix
so that you could set an index in that situation. While using theTINYINT
uses a single byte, so it can store 256 values, sufficient to store1
until12
. The year the same thing, usingSMALLINT
would use 2 bytes, and could set "2017" quietly.– Inkeliz
You don’t think using a date field would be more convenient?
– Anonimo