function DatabaseSelectSubqueryTestCase::testNotExistsSubquerySelect in Drupal 7
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 1883
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.');
}