SELECT * FROM ct_xxx
ORDER BY field(id,2,1,3) //示例代码
SELECT * FROM zhe_work_order
WHERE ( uid
= 221 ) AND zhe_work_order
.delete_time
IS NULL ORDER BY field(id,1798) desc
那么在thinkphp6.0中我们如何封装成这样的语句?
l i s t = W o r k O r d e r : : w h e r e ( " u i d " , list = WorkOrder::where("uid", list=WorkOrder::where("uid",user[“id”])
->orderRaw(“field(id,2,12,9”)
->select();
//或者 2
l i s t = W o r k O r d e r : : w h e r e ( " u i d " , list = WorkOrder::where("uid", list=WorkOrder::where("uid",user[“id”])
->orderRaw(“field(id,8”)
->select();
//或者3
Db::table(‘think_user’)
->where(‘status’, 1)
->orderRaw(“field(name,‘thinkphp’,‘onethink’,‘kancloud’)”)
->limit(5)
->select();
//或者 4
l i s t = W o r k O r d e r : : w h e r e ( " u i d " , list = WorkOrder::where("uid", list=WorkOrder::where("uid",user[“id”])
->orderRaw(“field(id,1587) desc”)
->select();
这种排序方式,是按照某一个字段,按照后面对应的数字,进行排序,在实用的场景,好像不常见。
二 ,在是用 多字段排序
->order([‘order’,‘id’=>‘desc’]) 以数组的形式排序,就出现了报错
报错内容 为
Column not found: 1054 Unknown column ‘order’ in 'order clause
当使用 order(‘type_name desc,id desc’) 这种方式进行多字段排序,就没有报错
还可以用这种方式 ->order(‘id desc’)->order(‘type_name desc’) 进行多字段排序。