查询语言.orderBy

Testing Is Documentation

tests/Database/Query/OrderByTest.php查询语言.orderBy - 图1

Uses

  1. <?php
  2. use Leevel\Database\Condition;
  3. use Tests\Database\DatabaseTestCase as TestCase;

orderBy 排序基础用法

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`tid` AS `id`,`test_query`.`tname` AS `value` FROM `test_query` ORDER BY `test_query`.`id` DESC,`test_query`.`name` ASC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query', 'tid as id,tname as value')
  16. ->orderBy('id DESC')
  17. ->orderBy('name')
  18. ->findAll(true)
  19. )
  20. );
  21. }

orderBy 指定表排序

  1. public function testWithTable(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`tid` AS `id`,`test_query`.`tname` AS `value` FROM `test_query` ORDER BY `test_query`.`id` DESC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query', 'tid as id,tname as value')
  16. ->orderBy('test_query.id DESC')
  17. ->findAll(true),
  18. 1
  19. )
  20. );
  21. }

orderBy 表达式排序

  1. public function testWithExpression(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT SUM(`test_query`.`num`),`test_query`.`tid` AS `id`,`test_query`.`tname` AS `value` FROM `test_query` ORDER BY SUM(`test_query`.`num`) ASC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query', '{SUM([num])},tid as id,tname as value')
  16. ->orderBy(Condition::raw('SUM([num]) ASC'))
  17. ->findAll(true),
  18. 2
  19. )
  20. );
  21. }

orderBy 表达式和普通排序混合

  1. public function testWithExpressionAndNormal(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`tid` AS `id`,`test_query`.`tname` AS `value` FROM `test_query` ORDER BY `test_query`.`title` ASC,`test_query`.`id` ASC,concat('1234',`test_query`.`id`,'ttt') DESC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query', 'tid as id,tname as value')
  16. ->orderBy('title,id,'.Condition::raw("concat('1234',[id],'ttt') desc"))
  17. ->findAll(true),
  18. 4
  19. )
  20. );
  21. }

orderBy 排序支持数组

  1. public function testWithArray(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`tid` AS `id`,`test_query`.`tname` AS `value` FROM `test_query` ORDER BY `test_query`.`title` ASC,`test_query`.`id` ASC,`test_query`.`ttt` ASC,`test_query`.`value` DESC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query', 'tid as id,tname as value')
  16. ->orderBy(['title,id,ttt', 'value desc'])
  17. ->findAll(true),
  18. 5
  19. )
  20. );
  21. }

orderBy 排序数组支持自定义升降

  1. public function testWithArrayAndSetType(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.`tid` AS `id`,`test_query`.`tname` AS `value` FROM `test_query` ORDER BY `test_query`.`title` DESC,`test_query`.`id` DESC,`test_query`.`ttt` ASC,`test_query`.`value` DESC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query', 'tid as id,tname as value')
  16. ->orderBy(['title,id,ttt asc', 'value'], 'desc')
  17. ->findAll(true),
  18. 6
  19. )
  20. );
  21. }

latest 快捷降序

  1. public function testLatest(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` ORDER BY `test_query`.`create_at` DESC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->latest()
  17. ->findAll(true)
  18. )
  19. );
  20. }

latest 快捷降序支持自定义字段

  1. public function testLatestWithCustomField(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` ORDER BY `test_query`.`foo` DESC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->latest('foo')
  17. ->findAll(true),
  18. 1
  19. )
  20. );
  21. }

oldest 快捷升序

  1. public function testOldest(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` ORDER BY `test_query`.`create_at` ASC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->oldest()
  17. ->findAll(true),
  18. 2
  19. )
  20. );
  21. }

oldest 快捷升序支持自定义字段

  1. public function testOldestWithCustomField(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` ORDER BY `test_query`.`bar` ASC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->oldest('bar')
  17. ->findAll(true),
  18. 3
  19. )
  20. );
  21. }

orderBy 表达式排序默认为升序

  1. public function testOrderByExpressionNotSetWithDefaultAsc(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` ORDER BY foo ASC",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->orderBy(Condition::raw('foo'))
  17. ->findAll(true)
  18. )
  19. );
  20. }