Cakephp MySQL 按查询分组


cakephp mysql group by query

在下面的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 );`

您可以通过以下方式执行此操作,但是我没有尝试过,但它会起作用。

$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 );`

或者,如果需要经常使用,也可以在模型中创建一个虚拟字段。