我的SQL查询:SELECT * ,COUNT(posts_id) as `view `FROM `post_view` JOIN posts ON post_view.posts_id = posts.id GROUP BY posts_id
我的雄辩Laravel:
$this->model->Join('posts','post_view.posts_id', '=', 'posts.id')
->selectRaw('*, count(posts_id) as view')
->groupBy('posts_id')
->get();
通过邮递员获取时出错:
SQLSTATE[42000]: Syntax error or access violation: 1055 'databasePost.post_view.id' isn't in GROUP BY
(SQL: select *, count(posts_id) as view from `post_view` inner join `posts` on `post_view`.`posts_id` =
`posts`.`id` group by `posts_id`)
发生此错误的原因group by
是查询的速度要求所有列。
我认为您使用的MySQL服务器版本为5.7.5+。看看这个:MySQL BY GROUP BY
用于ANY_VALUE
列或禁用mysql模式:ONLY_FULL_GROUP_BY.
->selectRaw('ANY_VALUE(post_view.id), ... count(posts_id) as view')
->groupBy('posts_id')
PS:post_view.id
和posts.id
两个命名id
,将其全部选中的是他们中的一个将被覆盖。
编辑您的应用程序的数据库配置文件 config/database.php
在mysql array中,设置strict => false
为禁用MySQL的严格模式:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
...
'strict' => false,
],
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句