Following the documentation on https://www.drupal.org/docs/7/api/database-api/dynamic-queries/joins I have constructed a very simple dynamic query with join:

$query = db_select('node', 'n')
     ->condition('n.uid', $promoter, '=')
     ->condition('n.type', 'campaign', '=')
     ->condition('n.status', '1', '=')
     ->fields('n', array('nid'));
     $query->join('field_data_field_department', 'd', 'n.nid = d.entity_id');

  $result = $query->execute();
    foreach ($result as $record) {

and it only returns nids whereas the expected results were nids together with the field_data_field_department field values. What should the correct query.


You're only selecting the nid field, you just need some more:

->fields('n', array('nid'))
->fields('d', array('fields', 'you', 'want'));
  • It is giving: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'd.field_data_field_department' in 'field list': SELECT n.nid AS nid, d.field_data_field_department AS field_data_field_department FROM {node} n INNER JOIN {field_data_field_department} d ON n.nid = d.entity_id WHERE... – Nick Jan 8 '17 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.