如何在magento中使用group by collection对列值求和(使用MAGENTO STANDARD)

a74*_*045 2 magento

我的桌子是这样的,我的收藏是 $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 
Run Code Online (Sandbox Code Playgroud)

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

       cat_id  rate   count_category
         1      9       3
         2      8       2 
Run Code Online (Sandbox Code Playgroud)

a74*_*045 5

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

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

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