簡體   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