title: Conditional joinLeft on magento collection
body: <p>My SQL query statement is as follows:</p>
  
  <pre><code>SELECT * FROM `sales_flat_order_grid` AS `main_table` 
  LEFT JOIN (SELECT * from `customer_address_entity_varchar` 
  WHERE attribute_id=31) AS `ce3` 
  ON main_table.customer_id=ce3.entity_id
  </code></pre>
  
  <p>and I need it to be translated into Magento style.
  I have tried </p>
  
  <pre><code>$collection = Mage::getResourceModel('sales/order_grid_collection'); 
  $tel = Mage::getModel('eav/entity_attribute')-&gt;loadByCode('2', 'telephone');
  
  $collection-&gt;getSelect()  
      -&gt;joinLeft(array('ce3' =&gt; 'customer_address_entity_varchar'), 'main_table.customer_id=ce3.entity_id', array('telephone' =&gt; 'value'))
      -&gt;where('ce3.attribute_id='.$tel-&gt;getAttributeId())
  </code></pre>
  
  <p>But this didn't work because the where statement is applied after the  join causing the total number of queries to be less than sales_flat_order_grid.</p>