繁体   English   中英

Laravel 5列既不包含在聚合函数中,也不包含在GROUP BY中

[英]Laravel 5 Column it is not contained in either an aggregate function or the GROUP BY

尝试使用Laravel合并ID并将其分组时,SQL出现错误。

选择列表中的“ push_notifications.title”列无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中

我一直在尝试使用最大和最小,但仍然出现错误。

这是我的代码:

$data  = $this->model->select( 'push_notifications.id', 'push_notifications.title', 'push_notifications.description', 'push_notifications.attachment', 'push_notifications.feature_id', 'push_notifications.target_id', 'features.title as feature', 'push_notifications.end_at')
            ->join('push_notification_users', 'push_notification_users.push_notification_id', '=', 'push_notifications.id')
            ->join('push_notification_roles', 'push_notification_roles.push_notification_id', '=', 'push_notifications.id')
            ->leftJoin('features', 'features.id', '=', 'push_notifications.feature_id')
            ->where( 'push_notifications.notif_target', 'homepage' )
            ->where(function($role) {
                $roles = RoleUser::where('user_id',$this->user->id)->first();
                $role->where( 'push_notification_roles.deleted_at', null )
                    ->where( 'push_notification_roles.role_id', $roles->role_id );
             })
            ->orWhere(function($user) {
                $user->where( 'push_notification_users.deleted_at', null )
                    ->where( 'push_notification_users.user_id', $this->user->id );
             })
            ->where( 'push_notifications.end_at', '>',  Carbon::now()->toDateTimeString() )
            ->groupBy( 'push_notifications.id' )
            ->get();

如果您有任何解决方案,请告诉我,我将不胜感激。 谢谢

找不到“ push_notifications.title”。 可能有一些拼写错误。 彻底检查。 如果不是这样,那么请附上表格结构的屏幕快照,即push_notifications。

另外一个原因是'push_notifications.id'必须有push_notifications.title的diff-diff值,并且groupBy中的单个'push_notifications.id'可以有多个'push_notifications.title'。 仅选择“ push_notifications.id”,其他一些唯一列将适用于您的情况。

请参阅此: https : //www.techonthenet.com/sql/group_by.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM