I have this SQL query: select main_table.*, (select nexway_status from nexway_order where order_id=main_table.entity_id ORDER BY id DESC LIMIT 1) as nexway_status from sales_flat_order_grid as main_table;

I need a collection for show the rows in grid orders..

I try this:

    $collection = Mage::getResourceModel($this->_getCollectionClass());
            array('n'=> 'nexway_order'),
            'n.order_id = main_table.entity_id',

But in that case it ignores the subselect, and I need it to do exactly what the above SQL query does, pick up a field with subselect..

Can you help me?


  • Try this $collection->getSelect()->order('n.id DESC'); $collection->getSelect()->group('n.order_id'); – Vinaya Maheshwari Apr 14 '18 at 5:33

Try following way:

$collection = Mage::getModel('sales/order')->getCollection();

$sub = $collection->getConnection()->select()->from(

->order('id DESC')->limit(1);

    ['nexway_status' => $sub]

echo  $collection->getSelectSql();
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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