This article describes the usage of the order () method of ThinkPHP. The order method can be used to sort database operation results. That is equivalent to an orderby clause in the select statement. This article describes the usage of the order () method of ThinkPHP. The order method can be used to sort database operation results. That is equivalent to an order by clause in the select statement.
The order method is one of the coherent operation methods of the model and is used to sort the results of database operations. That is equivalent to an order by clause in the select statement.
Usage
$Model->where('status=1')->order('id desc')->limit(5)->select();
Note: coherent operation methods have no sequence. You can change the call sequence before calling the select method.
Supports sorting multiple fields, for example:
$Model->where('status=1')->order('id desc,status')->limit(5)->select();
If the desc or asc sorting rule is not specified, the default value is asc.
If your field conflicts with the mysql keyword, we recommend that you use the array method, for example:
$Model->where('status=1')->order(array('order','id'=>'desc'))->limit(5)->select();
Supplement:
Thinkphp cannot use the-> order () sorting solution!
When ThinkPHP is used, you cannot use-> order ($ order) for sorting.
$ Order = "info. date2 desc ";
Unfortunately, the result order is changed to order by date2 limit... desc.
Solution 1:
$ Order cannot contain any space on both sides, $ order = "info. date2 desc"; (correct ). $ Order = "info. date2 desc"; (error !)
Solution 2:
Open the file: D: \ WebSite \ Zbphp.com \ www \ ThinkPHP \ Extend \ Model \ ViewModel. class. php
Modify row 136th to $ array = explode ('', trim ($ order), and add trim to save ,:
The second method is recommended, but we expect thinkphp official website to fix this small problem, so that you do not need to modify the kernel code yourself.
This article introduces how to use order () in ThinkPHP. I hope it will help you and thank you very much for your support for the script home website!