查询语言.union

Testing Is Documentation

tests/Database/Query/UnionTest.php查询语言.union - 图1

Uses

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

union 联合查询基本用法

  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` \nUNION SELECT `test_query`.`tid` AS `id`,`test_query`.`name` AS `value` FROM `test_query` WHERE `test_query`.`first_name` = :test_query_first_name\nUNION SELECT id,value FROM test_query WHERE id > 3\nUNION SELECT `test_query`.`tid` AS `id`,`test_query`.`name` AS `value` FROM `test_query` WHERE `test_query`.`first_name` = :test_query_first_name_1",
  7. {
  8. "test_query_first_name": [
  9. "222"
  10. ],
  11. "test_query_first_name_1": [
  12. "222"
  13. ]
  14. },
  15. false
  16. ]
  17. eot;
  18. $union1 = $connect
  19. ->table('test_query', 'tid as id,name as value')
  20. ->where('first_name', '=', '222');
  21. $union2 = 'SELECT id,value FROM test_query WHERE id > 3';
  22. $this->assertSame(
  23. $sql,
  24. $this->varJson(
  25. $connect
  26. ->table('test_query', 'tid AS id,tname as value')
  27. ->union($union1)
  28. ->union($union2)
  29. ->union($union1)
  30. ->findAll(true)
  31. )
  32. );
  33. $sql2 = <<<'eot'
  34. [
  35. "SELECT `test_query`.`tid` AS `id`,`test_query`.`tname` AS `value` FROM `test_query` \nUNION SELECT `test_query`.`tid` AS `id`,`test_query`.`name` AS `value` FROM `test_query` WHERE `test_query`.`first_name` = :test_query_first_name_2\nUNION SELECT id,value FROM test_query WHERE id > 3\nUNION SELECT `test_query`.`tid` AS `id`,`test_query`.`name` AS `value` FROM `test_query` WHERE `test_query`.`first_name` = :test_query_first_name_3",
  36. {
  37. "test_query_first_name_2": [
  38. "222"
  39. ],
  40. "test_query_first_name_3": [
  41. "222"
  42. ]
  43. },
  44. false
  45. ]
  46. eot;
  47. $this->assertSame(
  48. $sql2,
  49. $this->varJson(
  50. $connect
  51. ->table('test_query', 'tid as id,tname as value')
  52. ->union([$union1, $union2, $union1])
  53. ->findAll(true)
  54. )
  55. );
  56. }

TIP

参数支持字符串、子查询器以及它们构成的一维数组。

union 联合查询支持条件构造器自身为子查询

  1. public function testConditionSelfAsExpression(): 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` \nUNION SELECT `test_query`.`tid` AS `id`,`test_query`.`name` AS `value` FROM `test_query` WHERE `test_query`.`first_name` = :test_query_first_name",
  7. {
  8. "test_query_first_name": [
  9. "222"
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $union1 = $connect
  16. ->table('test_query', 'tid as id,name as value')
  17. ->where('first_name', '=', '222')
  18. ->databaseCondition();
  19. $this->assertSame(
  20. $sql,
  21. $this->varJson(
  22. $connect
  23. ->table('test_query', 'tid AS id,tname as value')
  24. ->union($union1)
  25. ->findAll(true)
  26. )
  27. );
  28. $sql2 = <<<'eot'
  29. [
  30. "SELECT `test_query`.`tid` AS `id`,`test_query`.`tname` AS `value` FROM `test_query` \nUNION SELECT `test_query`.`tid` AS `id`,`test_query`.`name` AS `value` FROM `test_query` WHERE `test_query`.`first_name` = :test_query_first_name_1\nUNION SELECT `test_query`.`tid` AS `id`,`test_query`.`name` AS `value` FROM `test_query` WHERE `test_query`.`first_name` = :test_query_first_name_2",
  31. {
  32. "test_query_first_name_1": [
  33. "222"
  34. ],
  35. "test_query_first_name_2": [
  36. "222"
  37. ]
  38. },
  39. false
  40. ]
  41. eot;
  42. $this->assertSame(
  43. $sql2,
  44. $this->varJson(
  45. $connect
  46. ->table('test_query', 'tid as id,tname as value')
  47. ->union([$union1, $union1])
  48. ->findAll(true),
  49. 2
  50. )
  51. );
  52. }

unionAll 联合查询不去重

  1. public function testUnionAll(): 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` \nUNION ALL SELECT id,value FROM test_query WHERE id > 1",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $union1 = 'SELECT id,value FROM test_query WHERE id > 1';
  12. $this->assertSame(
  13. $sql,
  14. $this->varJson(
  15. $connect
  16. ->table('test_query', 'tid as id,tname as value')
  17. ->unionAll($union1)
  18. ->findAll(true)
  19. )
  20. );
  21. }