简体   繁体   English

Laravel 中的 OrderBy 没有正确排序

[英]OrderBy in laravel doesn't correctly order

i have sql table called products and i want to order them according to price from low to high or vice versa我有一个名为 products 的 sql 表,我想根据价格从低到高订购它们,反之亦然

i have this code我有这个代码

$ts = Product::where('category_id',4)->where('status',5)->orderby('price','asc')->get();
    
     foreach($ts as $t){
         echo 'price => ' . $t->price.'<br>';
     }

i am printing the result to order price from low to high [ASC]我正在打印结果以从低到高订购价格 [ASC]

what i get is this我得到的是这个

price => 10.00
price => 10.00
price => 3.00
price => 3.00
price => 3.00
price => 3.00
price => 4.00
price => 4.00

how 10 become before 3 while in asc order按升序排列时 10 如何在 3 之前变成

If you see 10 before 3 in a order column, its being ordered as text and not a number.如果你看到103的顺序列,其被命令为文本,而不是数量。

SQL is about structured data, so when creating tables ensure they are of the type related to the data. SQL 是关于结构化数据的,因此在创建表时确保它们属于与数据相关的类型。 Dates are date or datetime etc. numbers are either int , decimal , float etc. If you don't, not only will ordering be hard, but also comparisons.日期是datedatetime等。数字是intdecimalfloat等。如果不这样做,不仅排序会很困难,而且比较也会很困难。 All SQL servers do well on operations related to data stored correctly.所有 SQL 服务器在与正确存储的数据相关的操作上都做得很好。

If in doubt, check the manual on datatypes available.如果有疑问,请查看有关可用数据类型手册

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

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