如果不改变sql模式=only_full_group_by模式,我如何在cakephp中执行group by?

cig3rfwq  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(130)

我正在尝试从交易表中得到每月的金额总和。我已经在下面写了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模式的情况下,如何在此处运行group by?

h9vpoimq

h9vpoimq1#

改为按聚合排序。
因为您是按月份分组的,所以这些组中的所有created字段都将属于同一个月份,例如,一个组中的所有created字段都将指向比另一个组中的字段更早或更晚的日期,因此您可以简单地从一个组中选择最小值或最大值:
第一个
此外,如果您将月份选择为数字而不是名称,则可以在该字段上进行排序。

相关问题