You are here

public function SelectSubqueryTest::testJoinSubquerySelect in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php \Drupal\KernelTests\Core\Database\SelectSubqueryTest::testJoinSubquerySelect()
  2. 9 core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php \Drupal\KernelTests\Core\Database\SelectSubqueryTest::testJoinSubquerySelect()

Tests that we can use a subquery in a JOIN clause.

File

core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php, line 191

Class

SelectSubqueryTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testJoinSubquerySelect() {

  // Create a subquery, which is just a normal query object.
  $subquery = $this->connection
    ->select('test_task', 'tt');
  $subquery
    ->addField('tt', 'pid', 'pid');
  $subquery
    ->condition('priority', 1);

  // Create another query that joins against the virtual table resulting
  // from the subquery.
  $select = $this->connection
    ->select('test', 't');
  $select
    ->join($subquery, 'tt', '[t].[id] = [tt].[pid]');
  $select
    ->addField('t', 'name');

  // The resulting query should be equivalent to:
  // @code
  // SELECT t.name
  // FROM test t
  //   INNER JOIN (SELECT tt.pid AS pid FROM test_task tt WHERE priority=1) tt ON t.id=tt.pid
  // @endcode
  $people = $select
    ->execute()
    ->fetchCol();
  $this
    ->assertCount(2, $people, 'Returned the correct number of rows.');
}