You are here

function SelectSubqueryTest::testJoinSubquerySelect in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SelectSubqueryTest.php \Drupal\system\Tests\Database\SelectSubqueryTest::testJoinSubquerySelect()

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

File

core/modules/system/src/Tests/Database/SelectSubqueryTest.php, line 103
Contains \Drupal\system\Tests\Database\SelectSubqueryTest.

Class

SelectSubqueryTest
Tests the Select query builder.

Namespace

Drupal\system\Tests\Database

Code

function testJoinSubquerySelect() {

  // Create a subquery, which is just a normal query object.
  $subquery = db_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 = db_select('test', 't');
  $select
    ->join($subquery, 'tt', 't.id=tt.pid');
  $select
    ->addField('t', 'name');

  // The resulting query should be equivalent to:
  // 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
  $people = $select
    ->execute()
    ->fetchCol();
  $this
    ->assertEqual(count($people), 2, 'Returned the correct number of rows.');
}