Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Order by price" returns a weird order in MySQL

Tags:

php

mysql

I have a local-only project which I'm working on where I have a table with id, title and price fields.

Example info:

ID || Title || Price
1 - Title 1 - 8.00
2 - Title 2 - 75.00
3 - Title 3 - 70.00

When I try to ORDER BY price it comes back like this:

8.00
75.00
70.00

Statement:

$query = mysql_query("Select * From table ORDER BY price DESC");

What am I doing wrong?

like image 221
rackemup420 Avatar asked Nov 28 '25 17:11

rackemup420


1 Answers

Your price column must have a character CHAR() or VARCHAR() type rather than a numeric type. Cast it as a DECIMAL in the ORDER BY:

Select * From table ORDER BY CAST(price AS DECIMAL(10,2)) DESC

The real fix for this would be to change the price data type to a proper numeric type.

like image 108
Michael Berkowski Avatar answered Nov 30 '25 07:11

Michael Berkowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!