如何在zend框架中一起使用COUNT和DISTINCT?

时间:2015-07-24 16:44:28

标签: php mysql zend-framework count zend-framework2

我正在尝试在zend

中重写以下查询
 SELECT COUNT(DISTINCT CPS.supplier_id, CPS.manufacturerid,CPS.categories_id)

            FROM suppliers_report AS CPS

            INNER JOIN  category_brand B ON B.categories_id = CPS.categories_id AND B.manufacturerid = CPS.manufacturerid

            INNER JOIN manufacturer m ON m.manufacturerid = CPS.manufacturerid

                          WHERE s.isactive=1 AND CPS.flg = 2 AND CPS.categories_id = c.parent_id

我在zend中尝试了以上查询

 $this->select()     
      ->setIntegrityCheck(false) 
            ->from(array('CPS' => 'suppliers_report'), array('CPS.supplier_id', 'CPS.manufacturerid', 'CPS.categories_id'))
            ->join(array('B' => 'category_brand'), 'B.categories_id=CPS.categories_id' AND 'B.manufacturerid = CPS.manufacturerid')  
            ->join(array('m' => 'manufacturer'), 'm.manufacturerid = CPS.manufacturerid')
            ->where('s.isactive=1 AND CPS.flg = 2 AND CPS.categories_id = c.parent_id AND CPS.manufacturerid=ctb.manufacturerid');

我在上述案例中坚持如何包含countDISTINCT。请帮助

1 个答案:

答案 0 :(得分:1)

您需要将Zend_Db_Expr用于COUNT()等函数,尝试类似以下内容:

 $this->select()     
      ->setIntegrityCheck(false) 
      ->from(array('CPS' => 'suppliers_report'), array(new Zend_Db_Expr('COUNT(DISTINCT CPS.supplier_id, CPS.manufacturerid,CPS.categories_id)')))
      ->join(array('B' => 'category_brand'), 'B.categories_id=CPS.categories_id' AND 'B.manufacturerid = CPS.manufacturerid')  
      ->join(array('m' => 'manufacturer'), 'm.manufacturerid = CPS.manufacturerid')
      ->where('s.isactive=1 AND CPS.flg = 2 AND CPS.categories_id = c.parent_id AND CPS.manufacturerid=ctb.manufacturerid');