Search code examples
drupaldrupal-6drupal-7

innerjoin query in drupal 7


I am applying this query for below D6 query , not working ..dont know wat wrong i'm doing ....does innerjoin fails in some condition

$result = db_select('px_slides','s')
    ->join('node','n','s.vid = n.vid')
    ->fields('s',array('tissue_type','body_site'))
    ->fields('n',array('sticky','title'))
    ->condition('n.status','1','=')
    ->condition('s.cid','126','=')
    ->execute()->fetchObject();

drupal 6 query i have:

    $result = db_query('
  SELECT n.nid, n.vid, n.sticky, n.title, n.created, s.cid, s.ref_id, s.viewurl, s.specimen_type, s.tissue_type, s.body_site, s.test_type, s.algorithm, s.result 
  FROM {px_slides} s INNER JOIN {node} n ON n.vid = s.vid
  WHERE n.status = 1 ')->execute();

Solution

  • You need to put your call to ->join() on a separate line altogether, as it doesn't return the query object:

    $query = db_select('px_slides','s')  
      ->fields('s',array('tissue_type','body_site'))
      ->fields('n',array('sticky','title'))
      ->condition('n.status','1','=')
      ->condition('s.cid','126','=');
    
    $query->join('node','n','s.vid = n.vid');
    
    $result = $query->execute()->fetchObject();