cakephp mysql group by query

时间:2012-07-06 05:13:28

标签: php mysql cakephp

在下面的cakephp mysql查询中,如何找到每次分组的行数?

$groupBy = 'PropertyViewer.ip_address';
$this->paginate = array(
  "group" => array( $groupBy ),
  "order" => array( "PropertyViewer.id desc" ) );
$view_info = $this->paginate( 'PropertyViewer', $conditions );
$this->set( 'view_info', $view_info );`

1 个答案:

答案 0 :(得分:1)

你可以通过以下方式实现,但是我没有尝试,但它会起作用。

$groupBy = 'PropertyViewer.ip_address'; 
$this->paginate = array( "fields" => (..., 'COUNT(PropertyViewer.id) AS total_rows),
                         "group" => array( $groupBy ),
                         "order" => array( "PropertyViewer.id desc" ) );
$view_info = $this->paginate( 'PropertyViewer', $conditions );
$this->set( 'view_info', $view_info );`

或者您也可以在模型中创建虚拟Field,如果您需要经常使用它。