簡體   English   中英

CakePHP 3 Query Builder - 如何在條件下使用 max 函數?

[英]CakePHP 3 Query Builder - How to use max function with a condition?

如何在條件下使用 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;

謝謝大家。

$query->select(['max_size' => $query->func()->max('size')])

使用 SQL 函數- http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM