【发布时间】:2016-12-31 10:40:04
【问题描述】:
此查询适用于 5.2:
$galleries = Gallery::with(array(
'images' => function ($query) {
$query->orderBy('order', 'asc');
}
))
->with('votes')
->leftJoin('votes', 'votes.votable_id', '=', 'gallery.id')
->selectRaw(
'gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status when "downvote" then 1 else null end) as points'
)
->where('votes.votable_type','App\Gallery')
->groupBy('gallery.id')
->orderBy('points', 'desc')
->published()->orderBy('gallery.created_at', 'desc')->paginate(30);
我正在尝试选择所有有投票的画廊,当我在 5.3 中运行它时,我得到了这个
1/2 PDOException in Connection.php line 333: SQLSTATE[42000]:
Syntax error or access violation: 1055 'images.gallery.title' isn't in GROUP BY
SQLSTATE[42000]: Syntax error or access violation: 1055 'images.gallery.title' isn't in
GROUP BY (SQL: select gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status when "downvote" then 1 else null end)
as points from `gallery` left join `votes` on `votes`.`votable_id` = `gallery`.`id`
where `votes`.`votable_type` = App\Gallery and `published` = 1 group by `gallery`.`id`
order by `points` desc, `gallery`.`created_at` desc limit 30 offset 0)
【问题讨论】:
-
您是否尝试将任务
gallery.title添加到GROUP BY子句中? -
是的,我试过了,然后它告诉我在我的画廊表中添加所有列,所以我最终得到了这个:->groupBy('gallery.id', 'gallery.title', 'gallery.hash', 'gallery.user_id', 'gallery.published', 'gallery.views' , 'gallery.created_at', 'gallery.updated_at') 但这对我来说不合适。你觉得对吗?
-
文档在升级指南中提到 5.3 已重写
join方法。 laravel.com/docs/5.3/upgrade#upgrade-5.3.0
标签: laravel eloquent laravel-5.2 laravel-5.3