【问题标题】:How to do a ORDER BY with an expression in Magento Collections如何使用 Magento Collections 中的表达式执行 ORDER BY
【发布时间】:2013-11-23 02:03:47
【问题描述】:

我已经知道如何使用 Magento 集合进行简单的订购。但是这次我想做这样的事情,

SELECT * FROM table WHERE filed LIKE '%abc%' 
ORDER BY  SUBSTRING(field, 1, 2) DESC LIMIT 10

那么如何在 order by 子句中添加 SUBSTRING 函数?有任何想法吗?

【问题讨论】:

    标签: sql magento collections sql-order-by


    【解决方案1】:

    Magento 模型有一个setOrder() 用于简单属性排序的方法:

    $oCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->setOrder('name_of_attribute_to_sort', 'ASC');
    

    他们没有像你这样按表达式排序的专用方法,但 Magento 集合使用扩展 Zend_Db_SelectVarien_Db_Select 实例,因此你可以使用它的 order()limit() 方法:

    $oCollection = Mage::getModel('catalog/product')->getCollection();
    $oCollection
        ->getSelect()
        ->order(array('SUBSTRING(field, 1, 2) DESC'))
        ->limit(10);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2017-10-06
      • 1970-01-01
      相关资源
      最近更新 更多