Group by not working - Laravel

主宰稳场 提交于 2019-11-26 16:14:24

问题


I'm not able to run this simple query in Laravel 5.3

$top_performers = DB::table('pom_votes')
        ->groupBy('performer_id')
        ->get();

It gives me:

SQLSTATE[42000]: Syntax error or access violation: 1055 'assessment_system.pom_votes.id' isn't in GROUP BY (SQL: select * from `pom_votes` group by `performer_id`)

However if I copy raw query from the error and fire directly in PhpMyAdmin, it works fine.

I have already checked this:

https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset

Any help would be appricaited.

Thanks,

Parth Vora


回答1:


Edit your applications's database config file config/database.php

In mysql array, set strict => false to disable MySQL's strict mode




回答2:


Maybe your issue is due to the fact that you are using a MySQL server vith version 5.7.5+. From this version on the way GROUP BY works is changed since they make it behave in order to be SQL99 compliant (where in previous versions it was not).

Try to do a full group by or change the configuration of your MySQL server.

Link to official MySQL doc where full GROUP BY is explanined




回答3:


Modify your database.php file from config/database.php 'strict' => true to 'strict' => false

    'mysql' => [ 
        'strict' => true 
    ],



回答4:


Edit your applications's database config file config/database.php

In mysql array, set strict => false to disable MySQL's strict mod

or

sudo nano /etc/mysql/mysql.cnf

[mysqld] sql_mode = NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION




回答5:


Go to config/database.php

Update strict value false.

return [
   'connections' => [
      'mysql' => [
         'strict' => false
       ]
   ]
]



回答6:


If you false strict mode then you can't use other strict functionality to fix this error Go to the Illuminate\Database\Connectors\MySqlConnector.php and change function like below:

protected function strictMode() {
return "set session
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY
_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'";
}

replace function with this.



来源:https://stackoverflow.com/questions/41571271/group-by-not-working-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!