聚合查询.aggregate

Testing Is Documentation

tests/Database/Read/AggregateTest.php聚合查询.aggregate - 图1

Uses

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

findCount 查询数量

  1. public function testCount(): 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. ->sql()
  16. ->table('test_query')
  17. ->findCount()
  18. )
  19. );
  20. }

count.find 查询数量

  1. public function testCount2(): 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. ->sql()
  16. ->table('test_query')
  17. ->count()
  18. ->find(),
  19. 1
  20. )
  21. );
  22. }

findCount 查询数量指定字段和别名

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

findAvg 查询平均值

  1. public function testAvg(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT AVG(`test_query`.`num`) 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. ->sql()
  16. ->table('test_query')
  17. ->findAvg('num')
  18. )
  19. );
  20. }

avg.find 查询平均值

  1. public function testAvg2(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT AVG(`test_query`.`num`) 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. ->sql()
  16. ->table('test_query')
  17. ->avg('num')
  18. ->find(),
  19. 1
  20. )
  21. );
  22. }

findAvg 查询平均值指定字段和别名

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

findMax 查询最大值

  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. ->sql()
  16. ->table('test_query')
  17. ->findMax('num')
  18. )
  19. );
  20. }

max.find 查询最大值

  1. public function testMax2(): 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. ->sql()
  16. ->table('test_query')
  17. ->max('num')
  18. ->find(),
  19. 1
  20. )
  21. );
  22. }

findMax 查询最大值指定字段和别名

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

findMin 查询最小值

  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. ->sql()
  16. ->table('test_query')
  17. ->findMin('num')
  18. )
  19. );
  20. }

min.find 查询最小值

  1. public function testMin2(): 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. ->sql()
  16. ->table('test_query')
  17. ->min('num')
  18. ->find(),
  19. 1
  20. )
  21. );
  22. }

findMin 查询最小值指定字段和别名

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

findSum 查询合计

  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. ->sql()
  16. ->table('test_query')
  17. ->findSum('num')
  18. )
  19. );
  20. }

sum.find 查询合计

  1. public function testSum2(): 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. ->sql()
  16. ->table('test_query')
  17. ->sum('num')
  18. ->find(),
  19. 1
  20. )
  21. );
  22. }

findSum 查询合计指定字段和别名

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

findAvg 查询字段指定表名

  1. public function testAvgWithTable(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT AVG(`test_query`.`num`) 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. ->sql()
  16. ->table('test_query')
  17. ->findAvg('test_query.num')
  18. )
  19. );
  20. }

avg.find 查询字段指定表名

  1. public function testAvgWithTable2(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT AVG(`test_query`.`num`) 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. ->sql()
  16. ->table('test_query')
  17. ->avg('test_query.num')
  18. ->find(),
  19. 1
  20. )
  21. );
  22. }