在magento getCollection中的情况

时间:2016-01-08 10:02:14

标签: mysql magento

我需要在magneto getcollection中执行以下查询,这是工作查询。

SELECT *
FROM `subscribers-members-old`
WHERE `ZIP` LIKE '9%'
ORDER BY 
    CASE WHEN `ZIP` = '9305' THEN 1 
        WHEN `ZIP` LIKE '9%' THEN 2 ELSE 3 END

但是我不知道如何在magento收集条件中添加案例,请你帮助我解决这个问题。

目前我的magento查询是

$collections = Mage::getModel('storelocator/storelocator')->getCollection()

                ->setStoreId($storeId)

                ->addFieldToFilter('status', 1);

$collections->addFieldToFilter('zipcode', array(
                array('where' => $zipcode), 
                array('like' => $trimzip[0].'%')

                ));

2 个答案:

答案 0 :(得分:1)

$collections->getSelect()->order
(new Zend_Db_Expr("CASE WHEN `ZIP` = '9305' THEN 1 
       WHEN `ZIP` LIKE '9%' THEN 2 
       ELSE 3 END"));

答案 1 :(得分:0)

$collections->getSelect()->order
("CASE WHEN `ZIP` = '9305' THEN 1 
       WHEN `ZIP` LIKE '9%' THEN 2 
       ELSE 3 END")