简体   繁体   中英

“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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM