DatabaseSelectSubqueryTestCase::testNotExistsSubquerySelect

7 database_test.test DatabaseSelectSubqueryTestCase::testNotExistsSubquerySelect()
8 database.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

modules/simpletest/tests/database_test.test, line 1765

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, t('NOT EXISTS query returned the correct results.'));
}
Login or register to post comments