How do I sort a date field in mysql?

0

How do I sort a date field entered by the datepicker script in the format dd-mm-yyyy in Mysql?

Note: Dates are being sorted only by day, ignoring the next month and year. And the date field is of type varchar (10).

Ex: 02-05-2018 (dd-mm-yyyy)

    
asked by anonymous 02.05.2018 / 15:28

1 answer

2

Because the date is in string it will sort the data by "-" , you need to convert the field to date

select 
  *
from tabela
  order by str_to_date(data, '%d-%m-%Y') desc

I've put an example in SQLFiddle for reference.

    
02.05.2018 / 15:53