在不改变 sql mode=only_full_group_by 模式的情况下,如何在 cakephp 中执行 group by?

Without changing sql mode=only_full_group_by mode how can I execute group by in cakephp?

我正在尝试从交易中获取按月计算的总金额 table。我在下面写了 cakephp 函数来获得我想要的输出。

public function getLastSixMOnthsExpenses($since_date, $t_type)
{
           $query = $this->find()->select([
                 'month' => $this->find()->func()->monthname([
                   'created' => 'identifier'
                ]),
               'amount' => $this->find()->func()->sum('Transactions.amount')
            ])
            ->where([
               'Transactions.created >='=>  $since_date->modify('6 months ago')->startOfMonth(),
               'Transactions.created <='=>  $since_date->endOfMonth(),
               'Transactions.transaction_type '=>$t_type
            ])
            ->group(['month'])
            ->order(['Transactions.created'=>'ASC'])
           ;
    
           return $query;
 }

我低于错误

Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'kjoumaa_kamaljoumaa.Transactions.created' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

不改变sql模式,我怎么运行在这里分组?

改为在聚合上订购。

由于您按月分组,这些组中的所有 created 字段都属于同一个月份,例如,一个组中的所有 created 字段将指向更早或更早的月份日期晚于另一组的字段,因此您可以简单地从一组中选择最小值或最大值:

->orderAsc(function (
    \Cake\Database\Expression\QueryExpression $exp,
    \Cake\ORM\Query $query
) {
    return $query->func()->min(
        $query->identifier('Transactions.created')
    );
})
ORDER BY MIN(Transactions.created) ASC

此外,如果您希望 select 将月份作为数字而不是名称,您可以在该字段上订购。