用于排序
<?php
// order 排序
public function order1()
{
$data = Db::table('wh_record')
->where([
['IDX', '>', 150000],
['UNIT', '=', 2],
['STATUS', 'IN', ['归还', '借出']],
])
->field('IDX AS ID, BUILD AS 阶段, ISN AS 序列号, STATUS AS 状态')
->order('IDX', 'DESC')
->page(1, 10)
->select();
return json($data);
}
支持多字段排序
<?php
// 支持多字段排序
public function order2()
{
$data = Db::table('wh_record')
->where([
['IDX', '>', 150000],
['UNIT', '=', 2],
['STATUS', 'IN', ['归还', '借出']],
])
->field('IDX AS ID, BUILD AS 阶段, ISN AS 序列号, STATUS AS 状态')
->order([
'IDX' => 'DESC',
'UNIT' => 'ASC'
])
->page(1, 10)
->select();
return json($data);
}
对于更新数据或者删除数据的时候可以用于优先级限制
<?php
Db::table('think_user')
->where('status', 1)
->order('id', 'desc')
->limit(5)
->delete();
生成的SQL
DELETE FROM `think_user` WHERE `status` = 1 ORDER BY `id` desc LIMIT 5
在V5.1.7+
版本开始,如果你需要在order
方法中使用mysql函数的话,必须使用下面的方式:
<?php
Db::table('think_user')
->where('status', 1)
->orderRaw("field(name,'thinkphp','onethink','kancloud')")
->limit(5)
->select();