rackemup420
rackemup420

Reputation: 1567

"Order by price" returns a weird order in 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?

Upvotes: 2

Views: 8377

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270609

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.

Upvotes: 10

Related Questions