magento - 如何在magento中使用group by集合对列值求和(使用MAGENTO标准)

标签 magento

我的 table 是这样的,我的收藏是 $testmodel=Mage::getModel(test/p1)->getCollection();

     my database table is below.

    id  cat_id  rate   p_id
     1    1      4      1
     2    1      2      1
     3    1      3      2
     4    2      5      3
     5    2      3      1 

我想在 magento 中使用集合输出这样的输出

       cat_id  rate   count_category
         1      9       3
         2      8       2 

最佳答案

我发现我的问题正确代码如下

$testmodel = Mage::getModel('test/p1')
                ->getCollection()
                ->addFieldToSelect('rate')
                ->addFieldToSelect('cat_id');

            $testmodel ->getSelect()
                ->columns('SUM(rate) as total,COUNT(*) AS countCategory')
                ->group('cat_id');

关于magento - 如何在magento中使用group by集合对列值求和(使用MAGENTO标准),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25389145/

相关文章:

Magento 结帐在 IE9 和 IE8 中被破坏

magento - 如何检查可配置产品是否缺货?

html - 如何修改 Magento Share Wishlist 电子邮件模板?

magento - 获取所有产品列表

mysql - Magento 在安装脚本中包含 .sql 文件

javascript - 如何将 js 文件添加/包含到 Magento 主题中

magento - 自定义属性类型,文件上传

jquery - Magento 可配置产品图像缩放问题

php - 多面搜索(solr)与通过 PHP 进行的旧式过滤比较好?

php - 使用 Magento 2.3.3 p1 后遇到的问题