• 查询语言.aggregate
    • 记录数量 count
    • 平均值 avg
    • 最大值 max
    • 最小值 min
    • 合计 sum

    查询语言.aggregate

    数据库聚合查询功能。

    引入相关类

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

    平均值 avg

    计算平均值。

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

    最大值 max

    计算最大值。

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

    最小值 min

    计算最小值。

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

    合计 sum

    计算合计。

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