2012-07-06 153 views
0

在下面的cakephp mysql查詢中,如何查找每次分組的行數?cakephp mysql group by query

$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

你可以像下面這樣做,但我沒有嘗試它,但它會工作。

$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,如果您需要頻繁使用它。