简体   繁体   English

Laravel,选择并分组-DB ::原始-问题

[英]Laravel, select and groupBy - DB::raw - issue

I've been trying to solve this, my first problem was this and its kind of working as expected. 我一直在尝试解决这个问题,我的第一个问题是这个问题及其按预期工作的方式。

The problem is that I can't select more columns than the ones in groupBy, so I've found a solution: DB::raw('created_at as created_at') 问题是我不能选择比groupBy中更多的列,所以我找到了一个解决方案: DB::raw('created_at as created_at')

But it's returning me this: 但这给了我:

SQLSTATE[42000]: Syntax error or access violation: 1055 created_at' isn't in GROUP BY

Ad if I add "create_at" groupBy(DB::raw('create_at')) and "concept" to groupBy, then they are grouped that is what I want to try to avoid. 广告,如果我将“ create_at” groupBy(DB::raw('create_at'))和“ concept”添加到groupBy,则将它们分组,这是我想要避免的。

My code is: 我的代码是:

@forelse($transaction->where('status', '!=', 'ok')
                      ->select('user_id', 'option_id', 'status', 'concept', DB::raw('created_at as created_at'), DB::raw('concept as concept'))
                      ->groupBy('user_id')
                      ->groupBy('option_id')
                      ->groupBy('status')
                      ->get() as $transaction)

{{$transaction->concept}}
{{$transaction->created_at}}

Thank you :) 谢谢 :)

Every column in a SELECT statement must also be in the GROUP BY clause. SELECT语句中的每一列也必须位于GROUP BY子句中。

There is a exception by setting the only_full_group_by mode to false in MySQL 5.7.5 or higher, but the assumption is the extra columns have the same value for all rows https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html 在MySQL 5.7.5或更高版本中,将only_full_group_by模式设置为false是一个例外,但是假设所有行的额外列都具有相同的值https://dev.mysql.com/doc/refman/5.7/en /group-by-handling.html

You can also accomplish this by using tricks to get a MAX() or MIN() for the non-grouped columns but you can not get the actual values of those columns for each row unless they are added to the GROUP BY statement 您也可以使用技巧为未分组的列获取MAX()或MIN(),但除非将它们添加到GROUP BY语句中,否则您将无法获得每一列的实际值,从而完成此操作

Mysql strict mode is enabled by default. 默认情况下启用Mysql严格模式。 Disable strict mode, it will solve your issue. 禁用严格模式,它将解决您的问题。

In config/database.php under 'mysql' 在config / database.php中的'mysql'

'strict' => true,

to false. 虚假。

'strict' => false,

Well, thanks to @noufalcep and @Rob Fonseca Rob, I came with this solution, I think this happens in Laravel after versión 5.4 where strict is set to true. 好吧,多亏了@noufalcep和@Rob Fonseca Rob,我提出了这个解决方案,我认为这是在5.4版本严格设置为true之后的Laravel中发生的。

So instead of disabling it completely I did this: 因此,我没有完全禁用它,而是这样做了:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            //'strict' => true, #####I COMMENTED THIS####
            'modes' => [
                        //'ONLY_FULL_GROUP_BY', #####AND THIS####
                        'STRICT_TRANS_TABLES',
                        'NO_ZERO_IN_DATE',
                        'NO_ZERO_DATE',
                        'ERROR_FOR_DIVISION_BY_ZERO',
                        'NO_AUTO_CREATE_USER',
                        'NO_ENGINE_SUBSTITUTION',
            ],
            'engine' => null,
        ],

By the way, MAX MIN didn't work for me, it was still requesting to add it into groupBy and asking for an array 顺便说一句,MAX MIN不适用于我,它仍在请求将其添加到groupBy中并请求一个数组

I hope this will help the next one. 希望这对下一个有所帮助。

Happy coding 快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM