Error message

You are browsing documentation for drupal 7.x, which is not supported anymore. Read the updated version of this page for drupal 11.x (the latest version).

function DatabaseSelectSubqueryTestCase::testExistsSubquerySelect

Test EXISTS subquery conditionals on SELECT statements.

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

File

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

Class

DatabaseSelectSubqueryTestCase
Test case for subselects in a dynamic SELECT query.

Code

function testExistsSubquerySelect() {
 // 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->exists ($subquery);
 $result = $query->execute ();
 // Ensure that we got the right record.
 $record = $result->fetch ();
 $this->assertEqual ($record->name , 'George', 'Fetched name is correct using EXISTS query.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.