1 database_test.test DatabaseSelectSubqueryTestCase::testNotExistsSubquerySelect()

Test 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/simpletest/tests/database_test.test, line 1835
Database tests.

Class

DatabaseSelectSubqueryTestCase
Test case for sub-selects in a dynamic SELECT query.

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.');
}