Zend Select按字段分组

时间:2012-08-28 09:09:11

标签: php mysql zend-framework zend-db-table

我的查询如下:

select status, count(id) as units from myTable group by status

我在使用的表格中有一个Zend_Db_Table对象,我正在尝试下面提到的代码段来创建上面的查询:

$objTable= new Application_Model_DbTable_MyTable();
$select = $objTable -> select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                    -> setIntegrityCheck(false); // i am also using joins to create the query, but this is not of concern
$select -> columns(array(
           'status' => new Zend_Db_Expr('DISTINCT(STATUS)'),
           'units'  => new Zend_Db_Expr('COUNT(id)')
           ));
$select -> group('status');

从上面的代码段创建的查询如下:

select myTable.*, DISTINCT(STATUS) as status, COUNT(id) as units from myTable group by status

我想从生成的查询中删除myTable.*

2 个答案:

答案 0 :(得分:1)

$select = $db ->select()
              ->distinct()
              ->from('myTable',array('status', 'COUNT(id) as units'))
              ->group('status');

答案 1 :(得分:0)

试试这个。

$select = new Zend_Db_Select;

$select->from(
             # table
             array(
                  't' => 'myTable'
             ),

             # columns
             array(
                 'status' => 'DISTINCT(STATUS)',
                 'units'  => 'COUNT(id)',
             ));
       ->group('status');

如果Zend_Db_Select子句中没有指定列

,问题是table.*会自动为您的查询添加->from(