function SelectSubqueryTest::testConditionSubquerySelect2

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php \Drupal\KernelTests\Core\Database\SelectSubqueryTest::testConditionSubquerySelect2()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php \Drupal\KernelTests\Core\Database\SelectSubqueryTest::testConditionSubquerySelect2()
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php \Drupal\KernelTests\Core\Database\SelectSubqueryTest::testConditionSubquerySelect2()

Tests we can use a subquery with a relational operator in a WHERE clause.

File

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

Class

SelectSubqueryTest
Tests the Select query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testConditionSubquerySelect2() {
    // Create a subquery, which is just a normal query object.
    $subquery = $this->connection
        ->select('test', 't2');
    $subquery->addExpression('AVG([t2].[age])');
    // Create another query that adds a clause using the subquery.
    $select = $this->connection
        ->select('test', 't');
    $select->addField('t', 'name');
    $select->condition('t.age', $subquery, '<');
    // The resulting query should be equivalent to:
    // SELECT t.name
    // FROM test t
    // WHERE t.age < (SELECT AVG(t2.age) FROM test t2)
    $people = $select->execute()
        ->fetchCol();
    $this->assertEqualsCanonicalizing([
        'John',
        'Paul',
    ], $people, 'Returned Paul and John.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.