1 database_test.test 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

core/modules/simpletest/tests/database_test.test, line 1805
Database tests.

Class

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