Magento Themes and Magento Extensions

How to create a sub-query on Magento Way?

Share

How to create a sub-query on Magento Way?
You can use the next examples:

public function getActiveCustomers($date)
{
    $orderTable = Mage::getResourceModel('sales/order_collection')
        ->addFieldToFilter('created_at', array('gt' => $date));
    $this->getSelect()
        ->joinLeft(array('order_table' => new Zend_Db_Expr( '(' . $orderTable->getSelect() . ')')),
'e.entity_id = order_table.customer_id')
        ->where('order_table.entity_id', 'notnull');
    return $this;
}
$this->getSelect()
    ->joinLeft
    (
        array('recommendationsyestable' => new Zend_Db_Expr( '(SELECT COUNT(*) as recommendationsyes, recy.review_id as rec_review_idy FROM ar_review_recommendations AS recy WHERE recy.status_id=2
AND recy.vote=2)')),
        'recommendationsyestable.rec_review_idy = rt.review_id'
    )
    ->joinLeft
    (
        array('recommendationsnotable' => new Zend_Db_Expr( '(SELECT COUNT(*)
 as recommendationsno, recn.review_id as rec_review_idn FROM ar_review_recommendations
AS recn WHERE recn.status_id=2 AND
recn.vote=1)')),
        'recommendationsnotable.rec_review_idn = rt.review_id'
    );
}

Comments are closed.