向雄辩的关系添加原始查询

aij0ehis  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(313)

我正在开发一个应用程序 Laravel 5.6 我有一个包含以下列的简单表:

company_id    project_id    company_role_id    company_specialisation_id

这代表了模型 AssociateCompanies ,与 company , project , role , specialisation 现在我有一些查询来获取属性:

$companies = AssociateCompany::whereHas('company', function ($q) use ($request) {
    $q->whereHas('projectOwners', function ($q) use($request) {
        $q->where('slug', $request->slug);
    });
})->groupBy('company_id', 'company_specialisation_id')->with('company', 'role', 'specialisation');

我想从两列中收集所有具有计数的唯一字段 company_id 以及 specialisation_id ,但是 groupBy 没有给我正确的结果,所以我不愿意继续:
sqlstate[42000]:语法错误或访问冲突:select list的1055表达式#1不在group by子句中,并且包含未聚合的列“conxn.project\u associate\u company.id”,该列在功能上不依赖group by子句中的列;这与sql\u mode=only\u full\u group\u by(sql:selectfrom)不兼容 project_associate_company 存在的位置(select,(select count()from) project_associate_company 哪里 companies . id = project_associate_company . company_id 以及 project_associate_company . deleted_at 为空)为 associated_projects_countcompanies 哪里 project_associate_company . company_id = companies . id 并且存在(从中选择projects 内部连接 project_owner_relationprojects . id = project_owner_relation . project_id 哪里 companies . id = project_owner_relation . company_id 以及 slug =lodha-patel-estate-tower-a-b-mumbai和 projects . deleted_at 为空)并且 companies . deleted_at 为空)并且 project_associate_company . deleted_at 分组依据为空 company_id , company_specialisation_id )"
所以我试着像这样运行原始查询:

$companies = AssociateCompany::whereHas('company', function ($q) use ($request) {
        $q->whereHas('projectOwners', function ($q) use($request) {
            $q->where('slug', $request->slug);
        });
    })->selectRaw(DB::raw('COUNT(*) AS count GROUP BY company_id , company_specialisation_id'))
    ->with('company', 'companyRole', 'specialisation')->get();

sqlstate[42000]:语法错误或访问冲突:1064您的sql语法有错误;请查看与您的mysql服务器版本对应的手册,以获取使用“groupbycompany\u id,company\u specialization\u id from”的正确语法 project_associate_company '在第1行(sql:select count()as count group by company\u id,company\u specialization\u id from' project_associate_company 存在的位置(select,(select count()from) project_associate_company 哪里 companies . id = project_associate_company . company_id 以及 project_associate_company . deleted_at 为空)为 associated_projects_countcompanies 哪里 project_associate_company . company_id = companies . id 并且存在(从中选择projects 内部连接 project_owner_relationprojects . id = project_owner_relation . project_id 哪里 companies . id = project_owner_relation . company_id 以及 slug =lodha-patel-estate-tower-a-b-mumbai和 projects . deleted_at 为空)并且 companies . deleted_at 为空)并且 project_associate_company . deleted_at “为空)”
告诉我更好的方法。谢谢。

jdgnovmf

jdgnovmf1#

最初我想注意的是,您不能在“select”语句中使用“groupby”。
因此不能聚合未聚合的列。这意味着分组字段可以有多个“角色”,所以你不能“加载”的“角色”。查询应类似于以下查询之一:

AssociateCompany::whereHas('company', function ($q) use ($request) {
    $q->whereHas('projectOwners', function ($q) use($request) {
        $q->where('slug', $request->slug);
    });
})->select('company_id', 'company_specialisation_id', \DB::raw('COUNT(*) as cnt'))
->groupBy('company_id', 'company_specialisation_id')
->with('company', 'specialisation');

或:

AssociateCompany::whereHas('company', function ($q) use ($request) {
    $q->whereHas('projectOwners', function ($q) use($request) {
        $q->where('slug', $request->slug);
    });
})->select('company_id', 'company_specialisation_id', 'company_role_id', \DB::raw('COUNT(*) as cnt'))
->groupBy('company_id', 'company_specialisation_id', 'company_role_id')
->with('company', 'specialisation', 'role');

我的建议是这样的,但我认为你可以用原始的mysql查询来解决它,而不是用它来雄辩。

相关问题