Можете ли вы выполнить запрос sub select внутри необработанного запроса в laravel?Если да, то где я иду не так?Вот мой запрос - PullRequest
0 голосов
/ 09 апреля 2019
DB::table('awarded_contracts as ac')
        ->select(
            DB::raw("COUNT(ac.id) AS numOfContracts"),
            DB::raw("SUM(ac.contract_amount) AS sumOfContractAmounts"),
            DB::raw("(SELECT COUNT(bid_submissions.id) from bid_submissions where bid_submissions.bid_invitation_id = ac.bid_invitation_id) AS numOfBidSubmissions"),
            DB::raw("(CASE WHEN bi.procurement_method_id ='0' THEN pm.title  ELSE pm2.title END) as method")

        )

        ->join('bid_invitations as bi', function ($q) {
            $q->on('bi.id', '=', 'ac.bid_invitation_id');
        })
        ->leftJoin('procurement_plan_entries as ppe', function ($q) {
        $q->on('ppe.id', '=', 'bi.procurement_plan_entry_id');
        })
        ->join('procurement_methods as pm', function ($q) {
            $q->on('ppe.procurement_method_id', '=', 'pm.id');
        })
        ->leftJoin('procurement_methods as pm2', function ($q) {
            $q->on('pm2.id', '=', 'bi.procurement_method_id');
        })

        ->groupBy('method')
        ->get();

Я хочу, чтобы мой подзапрос подсчитывал результаты в другой таблице, но запрос вызывает ошибку синтаксиса. вот ошибка, которую я получаю

SQLSTATE[42000]: Syntax error or access violation: 1055 'tenderpo_2019.ac.bid_invitation_id' isn't in GROUP BY (SQL: select COUNT(ac.id) AS numOfContracts, SUM(ac.contract_amount) AS sumOfContractAmounts, (SELECT COUNT(bid_submissions.id) from bid_submissions where bid_submissions.bid_invitation_id = ac.bid_invitation_id) AS numOfBidSubmissions, (CASE WHEN bi.procurement_method_id ='0' THEN pm.title  ELSE pm2.title END) as method from `awarded_contracts` as `ac` inner join `bid_invitations` as `bi` on `bi`.`id` = `ac`.`bid_invitation_id` left join `procurement_plan_entries` as `ppe` on `ppe`.`id` = `bi`.`procurement_plan_entry_id` inner join `procurement_methods` as `pm` on `ppe`.`procurement_method_id` = `pm`.`id` left join `procurement_methods` as `pm2` on `pm2`.`id` = `bi`.`procurement_method_id` left join `procurement_types` as `pt` on `pt`.`id` = `ppe`.`procurement_type_id` where `ac`.`financial_year` = 2018-2019 group by `method`)
...