简体   繁体   English

在 laravel 中进行表加入和分组

[英]Table join and group by in laravel

I am working on a API and I am using resource to return the collection.我正在处理 API,我正在使用资源返回集合。 I am having problem in grouping data.我在分组数据时遇到问题。 I have following code.我有以下代码。

return TicketResource::collection(
                Ticket::query()                    
                    ->search(request('search'))
                    ->whereHas(
                        'latestStatus', function($query) use ($request){
                            $query->whereIn('status_id', $request->status)                            
                            ->where(function($inner_query) use ($request){
                                if($request->status === 1){
                                   $inner_query->where( 'tickets.user_id', '!=', auth()->user()->id);
                                } elseif($request->status === 2){
                                    $inner_query->where( 'ticket_statuses.user_id', auth()->user()->id);
                                }
                            });
                        }
                    )
                    ->join('ticket_statuses', function($join){
                        $join->on('tickets.id', '=', 'ticket_statuses.ticket_id');
                    })
                    ->where(function ($query) use ($userBrands, $userAppliances, $buyerPurchasedTicket, $buyerexcludedZipCode, $buyerexcludedDevices) {
                        $query->whereIn('appliance_id', $userAppliances);
                        $query->whereIn('brand_id',  $userBrands);
                        // $query->whereNotIn('id', $buyerPurchasedTicket);
                        $query->whereNotIn('brand_id', $buyerexcludedDevices);
                        $query->whereNotIn('zip_code_id', $buyerexcludedZipCode);
                    })
                    ->when($request->filters['brand_ids'], fn ($q) => $q->whereIn('brand_id', $request->filters['brand_ids']))
                    ->when($request->filters['appliance_ids'], fn ($q) => $q->whereIn('appliance_id', $request->filters['appliance_ids']))
                    ->when($request->filters['province_ids'], fn ($q) => $q->whereIn('province_id', $request->filters['province_ids']))
                    ->when($request->price, fn ($q) => $q->whereBetween('price', [$request->min_price, $request->max_price]))
                    
                    ->orderBy('tickets.created_at', $request->order ?? 'desc')
                    ->groupBy('ticket_statuses.status_id')
                    ->paginate($request->rows ?? 30)
                )
            ->response()->getData(true);

When I run the above code I get the following error当我运行上面的代码时,出现以下错误

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'project.tickets.id' which is not functionally dependent on columns in GROUP BY clause; SQLSTATE[42000]:语法错误或访问冲突:1055 Expression #1 of SELECT 列表不在 GROUP BY 子句中并且包含非聚合列“project.tickets.id”,它在功能上不依赖于 GROUP BY 子句中的列; this is incompatible with sql_mode=only_full_group_by (SQL: select count( ) as aggregate from (select tickets . from tickets inner join ticket_statuses on tickets . id = ticket_statuses . ticket_id where exists (select * from ticket_statuses inner join (select MAX( ticket_statuses . id ) as id_aggregate , ticket_statuses . ticket_id from ticket_statuses group by ticket_statuses . ticket_id ) as latestOfMany on latestOfMany . id_aggregate = ticket_statuses . id and latestOfMany . ticket_id = ticket_statuses . ticket_id where tickets . id = ticket_statuses . ticket_id and status_id in (1, 2)) and ( appliance_id in (1, 2, 3) and brand_id in (1, 2, 3) and 1 = 1 and 1 = 1) group by ticket_statuses . status_id ) as aggregate_table )"这与 sql_mode=only_full_group_by (SQL: id count( ) 不兼容 as aggregate from ( tickets ticket_statuses tickets inner join ticket_statuses on tickets = id ticket_id exists (select * from ticket_statuses inner join (select MAX( ticket_statuses ) as id_aggregate , ticket_statuses . ticket_id from ticket_statuses group by ticket_statuses . ticket_id ) as latestOfMany on latestOfMany . id_aggregate = ticket_statuses . id and latestOfMany . ticket_id = ticket_statuses . ticket_id where tickets . id = ticket_statuses . ticket_id and status_id in (1, 2))和( appliance_id in (1, 2, 3) and brand_id in (1, 2, 3) and 1 = 1 and 1 = 1) group by ticket_statuses . status_id ) as aggregate_table )”

In your SQL query, your select list must only contains columns that are either appeared in the GROUP BY clause or aggregated columns.在您的 SQL 查询中,您的 select 列表必须仅包含出现在GROUP BY子句或聚合列中的列。

MySQL enables STRICT mode by default, because there would be many weird cases happened without it. MySQL 默认启用STRICT模式,因为没有它会发生很多奇怪的情况。 See an explanation of the STRICT mode here .在此处查看对STRICT模式的解释。

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

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