2015-09-26 101 views
2

如何在條件下使用max函數?CakePHP 3查詢生成器 - 如何在條件下使用max函數?

這就像在Translate SQL query to CakePHP 3.0下面的答案,但對我來說,我需要使用max函數和case子句(或條件的任何其他形式):

$options['contain'] = array('Users'); 
$query = $this->Pictures->find('all', $options); 
$query->select(['Users.username', 'Users.plan', 'Users.id']); 
$query->select(['sum' => $query->func()->sum('size'),]) 
     ->select(['count' => $query->func()->count('*'),]) 
     ->select(['facebook' => $query->func() 
      ->count('case when type = \'facebook\' then 1 else null end'),]) 
     ->select(['instagram' => $query->func() 
      ->count('case when type = \'instagram\' then 1 else null end'),]) 
     ->select(['device' => $query->func() 
      ->count('case when type = \'device\' then 1 else null end'),]) 
     ->group('user_id'); 

return $query; 

感謝所有。

回答

相關問題