简体   繁体   English

Zend按字段分组

[英]Zend Select group by Field

I have a query as below: 我有如下查询:

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

I have a Zend_Db_Table object at my displosal for the table in use, i am trying the below mentioned snippet to create the query above: 我对正在使用的表有一个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');

the query created from the above snippet is as follows: 从以上代码段创建的查询如下:

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

I want to remove the myTable.* from the query generated. 我想从生成的查询中删除myTable.*

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

Try this instead. 试试这个吧。

$select = new Zend_Db_Select;

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

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

The problem is Zend_Db_Select will automatically added table.* to your query if there's no column specified in ->from( clause 问题是Zend_Db_Select将自动将table.*添加到您的查询中,如果在->from(子句中未指定列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM