Dynamic query returns multiple data I have a quite simple query :
(find all nodes, fetch 3 fields from the node table and join the results with some tax.terms)

$query = db_select ( 'node', 'n' );
  $query-&gt;fields ( 'n', array( 
      'nid',
      'title',
      'type' ));
  $query-&gt;fields('b',array('body_value'));
  $query-&gt;fields('ttd',array('name'));

  $query-&gt;leftJoin ( 'field_data_body', 'b', 'b.entity_id = n.nid');
  $query-&gt;leftjoin ( 'taxonomy_index', 'ti', 'ti.nid = n.nid');
  $query-&gt;leftjoin ( 'taxonomy_term_data', 'ttd', 'ttd.tid = ti.tid AND ttd.vid = "5"');

  /* $query-&gt;condition('ttd',5); */

  $result = $query-&gt;execute ()-&gt;fetchAll ();


Now the problem is that i want a single object returned form the query for each node.
Like this :

stdClass Object
(
    [nid] =&gt; 557
    [title] =&gt; Lorem ipsum
    [type] =&gt; content_type
    [body_value] =&gt; lorem ispum
    [name] =&gt; taxonomy_term_value
)


But what i get is multiple objects because of the taxonomy index table (where a node can have anywhere from 12 term id's to none at all) but i only need 1 that corresponds to the condition stated above in the query (vid = 5, from the tax.term data table) if that node has a link to the tax.term, if the node does not have link it should return nothing,null,empty string...

So my results are as follows :

stdClass Object
(
    [nid] =&gt; 155
    [title] =&gt; lorem ipsum
    [type] =&gt; content_type
    [body_value] =&gt; lorem ispum
    [name] =&gt; 
)
stdClass Object
(
    [nid] =&gt; 3411
    [title] =&gt; lorem ipsum
    [type] =&gt; content_type
    [body_value] =&gt; lorem ispum
    [name] =&gt; value1
)
stdClass Object
(
    [nid] =&gt; 161
    [title] =&gt; lorem ipsum
    [type] =&gt; content_type
    [body_value] =&gt; lorem ispum
    [name] =&gt; value4
)
stdClass Object
(
    [nid] =&gt; 561
    [title] =&gt; lorem ipsum
    [type] =&gt; content_type
    [body_value] =&gt; lorem ispum
    [name] =&gt; value16
)
What you get currently is what you asked for: leftJoin.

To get only the single object you're looking for, you must use join instead (which is default and equivalent to innerJoin).

BTW you may simplify your code using the short notation, like this:

$query = db_select('node', 'n')
  -&gt;fields('n', array('nid', 'title', 'type' ))
  -&gt;fields('b', array('body_value'))
  -&gt;fields('ttd',array('name'))
  -&gt;join('field_data_body', 'b', 'b.entity_id = n.nid')
  -&gt;join('taxonomy_index', 'ti', 'ti.nid = n.nid')
  -&gt;join('taxonomy_term_data', 'ttd', 'ttd.tid = ti.tid AND ttd.vid = "5"');