CakePHP 3查询生成器 - 如何在条件下使用max函数?

时间:2021-08-21 16:53:19

How can I use max function with a condition?

如何在条件下使用max函数?

It's like the following answer in Translate SQL query to CakePHP 3.0, but in my case I need to use the max function and the case clause (or any other form of condition):

它类似于将SQL查询转换为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;

Thanks for all.

谢谢大家。

1 个解决方案

#1


1  

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

Using SQL Functions- http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions

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

#1


1  

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

Using SQL Functions- http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions

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