0

I need all nid array with multiple term field condition. I am using db_select() with join.

$query = db_select('node', 'n');
$query->innerjoin('field_data_field_condition', 'c', 'c.entity_id=n.nid');\
$query->join('field_data_field_skills', 's', 's.entity_id=n.nid');
$query->fields('n', array('nid', 'title', 'created'))
      ->condition(db_or()
      ->condition('c.field_condition_tid', 10)
      ->condition('s.field_skills_tid', 10)
        )
      ->condition('status', 1)
      ->condition('type', 'clinic')
      ->orderBy('created', 'DESC');
$result = $query->execute();
while ($node = $result->fetchAssoc()) {
 print_r($node);
}

This query doesn't show anything; I am using 10 as static value, just for example.

When I remove all joins and or condition, it shows the result like the following.

$query = db_select('node', 'n');
$query->fields('n', array('nid', 'title', 'created'))
->condition('status', 1)
->condition('type', 'clinic')
->orderBy('created', 'DESC');
$result = $query->execute();
while ($node = $result->fetchAssoc()) {
 print_r($node);
}
 Array (
  [nid] => 35
  [title] => Clinic 1
  [created] => 1486646343
  )
  Array
  (
  [nid] => 8
  [title] => Clinic 2
  [created] => 1485959105
  )
 Array
 (
 [nid] => 4
 [title] => Clinic 3
 [created] => 1485932987
 )

What am I doing wrong?

  • Try to use leftJoin instead of join and innerjoin. If joined table doesn't contain values result can be missed. – Andrew Nim Feb 22 '17 at 9:07
  • join is just a wrapper function for innerJoin. Just as a hint. – schlicki Feb 24 '17 at 13:11

Your Answer

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

Browse other questions tagged or ask your own question.