disordered months in mysql

3

my question is because the months in the database are disorderly and I want to know why they come out so I leave the code sql

SELECT mes_traslado FROM formulario_precebo 
WHERE año_destete = 2017 
GROUP BY mes_traslado;

and the result it gives me from the database is like this

1
10
11
12 
2
3
4
5
6
7 
8
9

I know there is a way to sort them but I want to know why they come out messy when doing this simple query

    
asked by Juan Esteban Yarce 04.04.2018 в 16:46
source

2 answers

2

Add the ORDER BY clause in the following way. Greetings.

SELECT mes_traslado*1 AS mes_traslado FROM formulario_precebo 
WHERE año_destete = 2017 
GROUP BY mes_traslado
ORDER BY 1 DESC;

Now, in order to give context to the solution proposed by multiplying the month_translated column by "1", the system interprets it as an integer and therefore when performing this type of "casting", utility is generated for the ORDER BY clause , the 1 in the ORDER BY is presented with the purpose of taking the first column that returns the query and is replaceable by the name of the column. About the use of Alias has the purpose of providing a clear label and not the operation in said label.

    
answered by 04.04.2018 / 16:47
source
0

Share DESC form_explore to see your table, the GROUP BY month_transfer will omit the duplicate months if this were the case, the ORDER BY month_transfer ASC | DESC should sort, if it does not, it should be because the field is varchar or another type string.

Your DESC would help to know if there is an autoIncremental id_table, yes:

  • Your 1 varchar corresponds to the AI (id_mes_translated, for example) 1
  • Your 2 varchar corresponds to the AI 2
  • ...
  • If so, you could use ORDER BY id_mes_traslado ASC | DESC     
    answered by 04.04.2018 в 17:04