2

I have a magento collection for which if i echo the getSelect() function, i get a query something like this:

   SELECT 
    `table1`.*,
    `table`.*,
    `table3.fieldy` AS 'fieldname'
  .....
  FROM ....
  WHERE....

and i would like to change this query into something like this:

   SELECT 
    `table1`.*,
    `table`.*,
    `table3.fieldy` AS 'fieldname',
     (SELECT col1, col2, (case when (action == 2 and state == 0) then 1 else 0 end) as state from tbl1)   AS 'fieldname2'
    ......
    FROM....
    WHERE....

now, for where clause for instance, you can add text to the query by using

productCollection->getSelect()->where('query text');

but how could I add custom query text to the first part of the select?

Many thanks

1
  • It depends. What specifically would you like to accomplish?
    – benmarks
    Feb 23, 2013 at 1:27

1 Answer 1

5

You can use Mage_Eav_Model_Entity_Collection_Abstract function addExpressionAttributeToSelect to add Subselect to your query.
Or just add new subselect field to select as:

$this->getSelect()->columns(array($alias => $fullExpression));

Note: $fullExpression should be wrapped in quotes.

2
  • Can you elaborate on the note about brackets? Is this like with joinField where you can use {{table}} to refer to the table you are joining? Sep 3, 2015 at 17:44
  • 1
    @Eric no, in this case we need to make sure that subselect is wrapped already. So it will result in following SELECT id, desc, (SUBSELECT) as alias FROM table
    – Sergey
    Sep 9, 2015 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.