How do I write a left join query? I want to get the value of "vid" knowing the node ID, and get the value of "name" for those rows where "vid" is equal to 2.

If it were a plain query, I would not know how to write it; I don't know how to write a left join query.

The "plain" SQL query in Drupal is the following:

$result = db_query('SELECT tid FROM {taxonomy_index} WHERE nid = :nid', array(':nid'=&gt;$nid));


The equivalent raw SQL query is the following:



SELECT did, name FROM taxonomy_index as ti LEFT JOIN taxonomy_term_data AS ttd ON ti.tid = ttd.tid WHERE vid = 2 and nid = $nid
$terms = db_select('taxonomy_index', 'ti')
  -&gt;fields('ti', array('tid', 'name'));
$terms-&gt;leftJoin('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid');
$terms-&gt;condition('vid', 2)
  -&gt;condition('nid', $nid)
  -&gt;execute();

while ($term = $terms-&gt;fetch()) {
  // $term contains the object for the taxonomy term.
}