function DatabaseSelectSubqueryTestCase::testNotExistsSubquerySelect

7.x 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

drupal-7.x/modules/simpletest/tests/database_test.test, line 1840

Class

DatabaseSelectSubqueryTestCase
Test case for subselects 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.');
}