简体   繁体   English

Laravel orderBy 没有返回正确的结果

[英]Laravel orderBy not returning correct results

My product model has range of products with values like 20.00 or 1000.00 however using Product::orderBy('price_1', 'desc')->take(10)->get();我的产品模型有一系列产品,其值如 20.00 或 1000.00,但使用Product::orderBy('price_1', 'desc')->take(10)->get(); the result does not show 1000.00 first it shows somewhere closer to the 20.00.结果首先不显示 1000.00,而是显示接近 20.00 的某个地方。 Is this because of the .这是因为. in the value or is there a better way to get the highest price first?在价值上还是有更好的方法首先获得最高价格?

Change the type of the column to DECIMAL(6,2) (or any other precision).将列的类型更改为DECIMAL(6,2) (或任何其他精度)。

The reason the fields are sorted like that is because it's a VARCHAR and varchars are sorted alphabetically, not by numeric value.字段像这样排序的原因是因为它是一个VARCHAR并且 varchars 按字母顺序排序,而不是按数值排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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