简体   繁体   中英

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

I got an error in SQL when trying to join and group the id using Laravel.

Column 'push_notifications.title' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I've been trying to use Max and Min but I still got an error.

Here's my code :

$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();

If you have any solution just tell Me, I will really appreciate. Thank You

'push_notifications.title' is not found. May there be some spelling mistake. Check thoroughly. If not so then please attach screenshot of your table structure ie push_notifications.

Also another reason is that there must be diff-diff values of push_notifications.title for 'push_notifications.id' and there can be multiple 'push_notifications.title' for single 'push_notifications.id' in groupBy. Only select 'push_notifications.id' and some other unique columns will work in your case.

refer this: https://www.techonthenet.com/sql/group_by.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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