语法错误或访问冲突:1055表达式#17 in group by

时间:2016-10-28 07:02:47

标签: php mysql laravel group-by

我在laravel 5.3中使用group尝试了一个查询。我抓住了

  

SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT列表的表达式#17不在GROUP BY子句中,并且包含非聚合列'testtravel.country.name',它在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_group_by(SQL:

)不兼容
select  `travel_request`.*, `travel_request`.`id` as `travel_id`,
        `department`.`name` as `dept_name`, `users`.`firstname` as `approver_name`,
        `travel_purpose`.`purpose`, `country`.`name` as `country_name`,
        `traveling_details`.`from_date`, `traveling_details`.`to_date`,
        `travel_request_status`.`status`
    from  `travel_request`
    inner join  `department`  ON `travel_request`.`department_id` = `department`.`id`
    inner join  `users`  ON `travel_request`.`approver_id` = `users`.`id`
    inner join  `travel_purpose`  ON `travel_request`.`travel_purpose_id` = `travel_purpose`.`id`
    inner join  `traveling_details`  ON `travel_request`.`id` = `traveling_details`.`travel_request_id`
    inner join  `country`  ON `country`.`id` = `traveling_details`.`country_id`
    inner join  `travel_request_status`  ON `travel_request`.`status_id` = `travel_request_status`.`id`
    where  `travel_request`.`approver_id` = 187
      and  `travel_request`.`status_id` != 4
    group by  `travel_request`.`id`
    limit  2 offset 0)

我复制了查询并在sql中运行。它在mysql中运行良好。我试过

 $users = DB::table('travel_request')
        ->join('department', 'travel_request.department_id', '=', 'department.id')
        ->join('users', 'travel_request.approver_id', '=', 'users.id')
        ->join('travel_purpose', 'travel_request.travel_purpose_id', '=', 'travel_purpose.id')
        ->join('traveling_details', 'travel_request.id','=','traveling_details.travel_request_id' )
        ->join('country','country.id', '=', 'traveling_details.country_id')
        ->join('travel_request_status','travel_request.status_id', '=', 'travel_request_status.id')
        ->select('travel_request.*', 'travel_request.id as travel_id','department.name as dept_name','users.firstname as approver_name','travel_purpose.purpose','country.name as country_name','traveling_details.from_date','traveling_details.to_date','travel_request_status.status')->where('travel_request.approver_id', $user_id)->where('travel_request.status_id','!=','4')->GROUPBY ('travel_request.id')->paginate(2);

解决方案

但是要禁用它,只需转到config / database.php并更改strict flag

'mysql' => [
            .
            .
            .
            'strict' => false,
            //'strict' => true,
            .
            .
        ],

3 个答案:

答案 0 :(得分:1)

打开config/database.php文件并更改mysql配置数组,如下所示:

来自'strict' => true

'strict' => false

请阅读此answer以获取有关错误的更多信息。

答案 1 :(得分:0)

您没有使用聚合函数,因此您可能正在使用group by来排序结果(或获取不同的值)    从mysql 5.6开始使用group只是指定了涉及组的所有列,并且不可能是部分规范。

还使用group by进行订购

因此,如果您使用的是订购,请使用正确的订单,而不是按

分组
  select 
        `travel_request`.*
      , `travel_request`.`id` as `travel_id`
      , `department`.`name` as `dept_name`
      , `users`.`firstname` as `approver_name`
      , `travel_purpose`.`purpose`
      , `country`.`name` as `country_name`
      , `traveling_details`.`from_date`
      , `traveling_details`.`to_date`
      , `travel_request_status`.`status` 
  from `travel_request` 
  inner join `department` on `travel_request`.`department_id` = `department`.`id` 
  inner join `users` on `travel_request`.`approver_id` = `users`.`id` 
  inner join `travel_purpose` on `travel_request`.`travel_purpose_id` = `travel_purpose`.`id` 
  inner join `traveling_details` on `travel_request`.`id` = `traveling_details`.`travel_request_id` 
  inner join `country` on `country`.`id` = `traveling_details`.`country_id` 
  inner join `travel_request_status` on `travel_request`.`status_id` = `travel_request_status`.`id` 
  where `travel_request`.`approver_id` = 187 and `travel_request`.`status_id` != 4 
  order by `travel_request`.`id` limit 2 offset 0)

此行为是由sql_mode=only_full_group_by控制的,您可以更改此参数以使用旧行为https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

答案 2 :(得分:0)

这是解决方案并且正常工作:) 打开终端和

sudo mysql -uroot -ppassword

并为您的 MySQL 服务器实例更改 SQL 模式:

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));