You are here

function SelectSubqueryTest::testNotExistsSubquerySelect 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::testNotExistsSubquerySelect()

Tests NOT EXISTS subquery conditionals on SELECT statements.

We essentially select all rows from the {test} table that don't have matching rows in the {test_people} table based on the shared name column.

File

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

Class

SelectSubqueryTest
Tests the Select query builder.

Namespace

Drupal\system\Tests\Database

Code

function testNotExistsSubquerySelect() {

  // Put George into {test_people}.
  db_insert('test_people')
    ->fields(array(
    'name' => 'George',
    'age' => 27,
    'job' => 'Singer',
  ))
    ->execute();

  // Base query to {test}.
  $query = db_select('test', 't')
    ->fields('t', array(
    'name',
  ));

  // Subquery to {test_people}.
  $subquery = db_select('test_people', 'tp')
    ->fields('tp', array(
    'name',
  ))
    ->where('tp.name = t.name');
  $query
    ->notExists($subquery);

  // Ensure that we got the right number of records.
  $people = $query
    ->execute()
    ->fetchCol();
  $this
    ->assertEqual(count($people), 3, 'NOT EXISTS query returned the correct results.');
}