用于排序

    1. <?php
    2. // order 排序
    3. public function order1()
    4. {
    5. $data = Db::table('wh_record')
    6. ->where([
    7. ['IDX', '>', 150000],
    8. ['UNIT', '=', 2],
    9. ['STATUS', 'IN', ['归还', '借出']],
    10. ])
    11. ->field('IDX AS ID, BUILD AS 阶段, ISN AS 序列号, STATUS AS 状态')
    12. ->order('IDX', 'DESC')
    13. ->page(1, 10)
    14. ->select();
    15. return json($data);
    16. }

    支持多字段排序

    1. <?php
    2. // 支持多字段排序
    3. public function order2()
    4. {
    5. $data = Db::table('wh_record')
    6. ->where([
    7. ['IDX', '>', 150000],
    8. ['UNIT', '=', 2],
    9. ['STATUS', 'IN', ['归还', '借出']],
    10. ])
    11. ->field('IDX AS ID, BUILD AS 阶段, ISN AS 序列号, STATUS AS 状态')
    12. ->order([
    13. 'IDX' => 'DESC',
    14. 'UNIT' => 'ASC'
    15. ])
    16. ->page(1, 10)
    17. ->select();
    18. return json($data);
    19. }

    对于更新数据或者删除数据的时候可以用于优先级限制

    1. <?php
    2. Db::table('think_user')
    3. ->where('status', 1)
    4. ->order('id', 'desc')
    5. ->limit(5)
    6. ->delete();

    生成的SQL

    1. DELETE FROM `think_user` WHERE `status` = 1 ORDER BY `id` desc LIMIT 5

    V5.1.7+版本开始,如果你需要在order方法中使用mysql函数的话,必须使用下面的方式:

    1. <?php
    2. Db::table('think_user')
    3. ->where('status', 1)
    4. ->orderRaw("field(name,'thinkphp','onethink','kancloud')")
    5. ->limit(5)
    6. ->select();