查询语言.orderBy

函数原型

  1. public function order($mixExpr);

说明:参数支持字符串以及它们构成的一维数组。

用法如下

  1. # SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test` ORDER BY `test`.`id` DESC,`test`.`name` ASC
  2. Db::table('test', 'tid as id,tname as value')->
  3. orderBy('id DESC')->
  4. orderBy('name')->
  5. getAll();
  6. # SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test` ORDER BY `test`.`id` DESC
  7. Db::table('test', 'tid as id,tname as value')->
  8. orderBy('test.id DESC')->
  9. getAll();
  10. # SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test` ORDER BY SUM(`test`.`num`) ASC
  11. Db::table('test', 'tid as id,tname as value')->
  12. orderBy('{SUM([num]) ASC}')->
  13. getAll();
  14. # SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test` ORDER BY `test`.`title` ASC,`test`.`id` ASC,concat('1234',`test`.`id`,'ttt') DESC
  15. Db::table('test', 'tid as id,tname as value')->
  16. orderBy("title,id,{concat('1234',[id],'ttt') desc}")->
  17. getAll();
  18. # SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test` ORDER BY `test`.`title` ASC,`test`.`id` ASC,`test`.`ttt` ASC,`test`.`value` DESC
  19. Db::table('test', 'tid as id,tname as value')->
  20. orderBy(['title,id,ttt', 'value desc'])->
  21. getAll();
  22. # SELECT `test`.`tid` AS `id`,`test`.`tname` AS `value` FROM `test` ORDER BY `test`.`title` DESC,`test`.`id` DESC,`test`.`ttt` ASC,`test`.`value` DESC
  23. Db::table('test', 'tid as id,tname as value')->
  24. orderBy(['title,id,ttt asc', 'value'], 'desc')->
  25. getAll();

快捷排序 latest/oldest

  1. # SELECT `test`.* FROM `test` ORDER BY `test`.`create_at` DESC
  2. Db::->table('test')->
  3. latest()->
  4. getAll();
  5. # SELECT `test`.* FROM `test` ORDER BY `test`.`foo` DESC
  6. Db::->table('test')->
  7. latest('foo')->
  8. getAll();
  9. # SELECT `test`.* FROM `test` ORDER BY `test`.`create_at` ASC
  10. Db::->table('test')->
  11. oldest()->
  12. getAll();
  13. # SELECT `test`.* FROM `test` ORDER BY `test`.`bar` ASC
  14. Db::->table('test')->
  15. oldest('bar')->
  16. getAll();