“按价格订购"在 MySQL 中返回一个奇怪的顺序 [英] "Order by price" returns a weird order in MySQL

查看:40
本文介绍了“按价格订购"在 MySQL 中返回一个奇怪的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仅限本地的项目,我正在处理一个包含 idtitleprice 字段的表格.

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

示例信息:

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

当我尝试ORDER BY price时,它会像这样返回:

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

8.00
75.00
70.00

声明:

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

我做错了什么?

推荐答案

您的 price 列必须具有字符 CHAR() 或 VARCHAR() 类型而不是数字类型.将其转换为 ORDER BY 中的 DECIMAL:

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

真正的解决方法是将 price 数据类型更改为正确的数字类型.

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

这篇关于“按价格订购"在 MySQL 中返回一个奇怪的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆