laravel 5.5 group by on single column不起作用

时间:2017-09-20 07:40:45

标签: php mysql laravel laravel-5.5

即使我将设置“严格”设置为假,单列上的laravel 5.5 group by也不起作用..我已经在mysql工作台中尝试了原始查询并且它工作正常但是我在laravel上有这个错误:

SQLSTATE[42000]: Syntax error or access violation: 1055 'hammer.vehicle.consignor_id' isn't in GROUP BY (SQL: select `vehicle`.*, `vehicle_brand`.`vehicle_brand_name`, `vehicle_model`.`vehicle_model_name`, `vehicle_variant`.`vehicle_variant_name`, `vehicle_model`.`year`, `vehicle`.`vehicle_id` from `vehicle` left join `vehicle_brand` on `vehicle_brand`.`vehicle_brand_id` = `vehicle`.`vehicle_brand_id` left join `vehicle_model` on `vehicle_model`.`vehicle_brand_id` = `vehicle_brand`.`vehicle_brand_id` left join `vehicle_variant` on `vehicle_variant`.`vehicle_model_id` = `vehicle_model`.`vehicle_model_id` group by `vehicle`.`vehicle_id` limit 10 offset 0)

这是导致错误的laravel代码

\DB::table('vehicle')
                    ->select('vehicle.*', 'vehicle_brand.vehicle_brand_name', 'vehicle_model.vehicle_model_name', 'vehicle_variant.vehicle_variant_name', 'vehicle_model.year', 'vehicle.vehicle_id')
                    ->leftJoin('vehicle_brand', 'vehicle_brand.vehicle_brand_id', '=', 'vehicle.vehicle_brand_id')
                    ->leftJoin('vehicle_model', 'vehicle_model.vehicle_brand_id', '=', 'vehicle_brand.vehicle_brand_id')
                    ->leftJoin('vehicle_variant', 'vehicle_variant.vehicle_model_id', '=', 'vehicle_model.vehicle_model_id')
                    ->where($where)
                    ->groupBy('vehicle.vehicle_id')
                    ->paginate(10);

配置“strict”中的mysql设置已设置为false

1 个答案:

答案 0 :(得分:7)

我认为导致问题的是严格模式,检查config/database.php并检查mysql内部,然后更改

'strict' => true,

'strict' => false,

因为如thread

所述
  

如果设置为true,那么它会在ONLY_FULL_GROUP_BY时添加ONLY_FULL_GROUP_BY   查询。

或者您可以尝试通过执行以下操作来禁用only_full_group_by设置:

mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql> 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';

根据建议here