查询语言.groupBy

Testing Is Documentation

tests/Database/Query/GroupByTest.php查询语言.groupBy - 图1

groupBy 函数原型

  1. public function groupBy($expression);
  • 参数支持字符串以及它们构成的一维数组,用法和 《查询语言.orderBy》 非常相似。

Uses

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

groupBy 基础用法

  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` GROUP BY `test_query`.`id`,`test_query`.`name`",
  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. ->groupBy('id')
  17. ->groupBy('name')
  18. ->findAll(true)
  19. )
  20. );
  21. }

groupBy 字段指定表名

  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` GROUP BY `test_query`.`id`",
  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. ->groupBy('test_query.id')
  17. ->findAll(true),
  18. 1
  19. )
  20. );
  21. }

groupBy 字段表达式

  1. public function testWithExpression(): 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` GROUP BY `test_query`.`num` HAVING SUM(`test_query`.`num`) > :SUM_test_query_num",
  7. {
  8. "SUM_test_query_num": [
  9. 9
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->table('test_query', 'tid as id,tname as value')
  20. ->groupBy(Condition::raw('[num]'))
  21. ->having(Condition::raw('SUM([num])'), '>', 9)
  22. ->findAll(true),
  23. 2
  24. )
  25. );
  26. }

groupBy 复合型

  1. public function testWithComposite(): 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` GROUP BY `test_query`.`title`,`test_query`.`id`,concat('1234',`test_query`.`id`,'ttt')",
  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. ->groupBy('title,id,'.Condition::raw("concat('1234',[id],'ttt')"))
  17. ->findAll(true),
  18. 3
  19. )
  20. );
  21. }

groupBy 字段数组支持

  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` GROUP BY `test_query`.`title`,`test_query`.`id`,`test_query`.`ttt`,`test_query`.`value`",
  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. ->groupBy(['title,id,ttt', 'value'])
  17. ->findAll(true),
  18. 4
  19. )
  20. );
  21. }