您可以在laravel的原始查询中运行子选择查询吗?如果是,我哪里错了?这是我的查询

时间:2019-04-09 09:51:27

标签: mysql laravel eloquent eager-loading

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`)

0 个答案:

没有答案