Comparison between decimal numbers

Asked

Viewed 212 times

-1

I have 2 posts on a table, with prices 500,00 and 88,98. When I run to sort according to the highest price, Mysql returns me that 88.98 is higher than 500.00, the "price" field is a string, because it happens?

  • Can you put the code you made?

  • And why preço is varchar?

2 answers

2

Because 5 comes before 8, as is string it is comparing the characters. Change to decimal the data type.

  • To which type? I changed to decimal and had it: Truncated DECIMAL value '5.00'

2

You can follow what Douglas said, from the table extrusion change the data type for this field. Also you can convert from select by fetching the data using this syntax:

SELECT seu_campo FROM `sua_tabela`
ORDER BY CONVERT(seu_campo ,DECIMAL(10,2)) ASC
;
  • Thanks, I’ll test, but what would this 10.2?

  • 10,2 are the attributes of the decimal data type. Where: 10-Represents the numbers before the comma 2-Represents the numbers after the comma (decimal place)

Browser other questions tagged

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