查询语言.aggregate

Testing Is Documentation

tests/Database/Query/AggregateTest.php查询语言.aggregate - 图1

数据库聚合查询功能。

Uses

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

记录数量 count

计算记录数量。

函数原型

  1. public function findCount(string $field = '*', string $alias = 'row_count', bool $flag = false);

TIP

可使用 findCount() 或者 count()->find() 来统计记录行。

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT COUNT(*) AS row_count FROM `test_query` LIMIT 1",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->count()
  17. ->findOne(true)
  18. )
  19. );
  20. $sql = <<<'eot'
  21. [
  22. "SELECT COUNT(`test_query`.`id`) AS row_count FROM `test_query` LIMIT 1",
  23. [],
  24. false
  25. ]
  26. eot;
  27. $this->assertSame(
  28. $sql,
  29. $this->varJson(
  30. $connect
  31. ->table('test_query')
  32. ->count('id')
  33. ->findOne(true),
  34. 1
  35. )
  36. );
  37. $sql = <<<'eot'
  38. [
  39. "SELECT COUNT(`test_query`.`id`) AS count1 FROM `test_query` LIMIT 1",
  40. [],
  41. false
  42. ]
  43. eot;
  44. $this->assertSame(
  45. $sql,
  46. $this->varJson(
  47. $connect
  48. ->table('test_query')
  49. ->count('id', 'count1')
  50. ->findOne(true),
  51. 2
  52. )
  53. );
  54. $sql = <<<'eot'
  55. [
  56. "SELECT COUNT(`test_query`.`id`*50) AS count1 FROM `test_query` LIMIT 1",
  57. [],
  58. false
  59. ]
  60. eot;
  61. $this->assertSame(
  62. $sql,
  63. $this->varJson(
  64. $connect
  65. ->table('test_query')
  66. ->count(Condition::raw('[id]*50'), 'count1')
  67. ->findOne(true),
  68. 3
  69. )
  70. );
  71. }

平均值 avg

计算平均值。

  1. public function testAvg(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT AVG(`test_query`.`id`) AS avg_value FROM `test_query` LIMIT 1",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->avg('id')
  17. ->findOne(true)
  18. )
  19. );
  20. }

最大值 max

计算最大值。

  1. public function testMax(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT MAX(`test_query`.`num`) AS max_value FROM `test_query` LIMIT 1",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->max('num')
  17. ->findOne(true)
  18. )
  19. );
  20. }

最小值 min

计算最小值。

  1. public function testMin(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT MIN(`test_query`.`num`) AS min_value FROM `test_query` LIMIT 1",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->min('num')
  17. ->findOne(true)
  18. )
  19. );
  20. }

合计 sum

计算合计。

  1. public function testSum(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT SUM(`test_query`.`num`) AS sum_value FROM `test_query` LIMIT 1",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->sum('num')
  17. ->findOne(true)
  18. )
  19. );
  20. }