【问题标题】:I want to convert my raw query to query builder我想将原始查询转换为查询生成器
【发布时间】:2019-11-03 00:36:34
【问题描述】:

我是 laravel 的新手 实际上我有一个身份验证表,它根据用户类型链接到另一个表。我想要来自相应表的用户信息,所以我现在使用原始查询我想将其转换为查询生成器,请帮助

$data = DB::select("SELECT srr.id,srr.created_at,srr.fromid,srr.toid,srr.from_usertype,au.firstname_admin,au.lastname_admin,cd.name as to_compayname,COALESCE(unionSub1.firstname,NULL) as from_firstname, unionSub1.lastname as from_lastname
                 from service_request_reviews as srr
                 left join (
                     (select authid, firstname, lastname from userdetails)
                     union (select authid, firstname, lastname from yachtdetail)
                     union (select authid, firstname, lastname from talentdetails)
                     union (select authid, name as firstname, COALESCE(NULL,NULL) as lastname from companydetails)
                 ) unionSub1 on unionSub1.authid = srr.fromid
                 left join auths as au on au.id = srr.fromid
                 LEFT JOIN companydetails as cd ON cd.authid = srr.toid WHERE srr.isdeleted = '0' AND srr.parent_id1 = '0' " );

我已经尝试过这个并且它在没有联合的情况下工作正常。我不知道如何在左连接中使用多个联合。

$data = DB::table('service_request_reviews as srr')
       ->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
                ->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
 ->where('srr.isdeleted', '0')
 ->where('srr.parent_id', '0');

【问题讨论】:

    标签: laravel postgresql query-builder


    【解决方案1】:

    您可以将联合定义为该表的查询构建器,例如:

    $yachtdetail = DB::table("yachtdetail")
                        ->select('authid', 'firstname', 'lastname');
    
    $talentdetails = DB::table('talentdetails')
                        ->select('authid', 'firstname', 'lastname');
    
    

    现在你可以像这样使用:

    $data = DB::table('service_request_reviews as srr')
           ->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
           ->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
           ->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
           ->where('srr.isdeleted', '0')
           ->where('srr.parent_id', '0')
           ->union($yachtdetail)
           ->union($talentdetails)
           ->get();
    
    

    这是文档的链接。 https://laravel.com/docs/5.8/queries#unions

    已编辑:

    在你的情况下,你可以尝试这样的事情:

    $queryBuilder = DB::table('service_request_reviews as srr')
           ->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
           ->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
           ->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
           ->leftjoin(DB::raw("((select authid, firstname, lastname from userdetails)
                         union (select authid, firstname, lastname from yachtdetail)
                         union (select authid, firstname, lastname from talentdetails)
                         union (select authid, name as firstname, null as lastname from companydetails)) as unionSub1"), function($join){
    
                            $join->on(DB::raw('unionSub1.authid'), '=', DB::raw('srr.fromid'));
           })
           ->where('srr.isdeleted', '0')
           ->where('srr.parent_id', '0');
    
    $data = $queryBuilder->get();
    
    

    【讨论】:

    • 感谢您的回复,但我想转换此 ``` left join ( (select authid, firstname, lastname from userdetails) union (select authid, firstname, lastname from Yachtdetail) union (select authid, firstname, lastname from Talentdetails) union (select authid, name as firstname, COALESCE(NULL,NULL) as lastname from companydetails) ) unionSub1 on unionSub1.authid = srr.fromid ``` 查询生成器的原始查询
    • @DeekshaRana 请查看Edited 部分
    • 再次感谢您的回复,我在编辑查询时收到此错误 ``` 消息:“SQLSTATE[42601]:语法错误:7 错误:FROM 中的子查询必须有别名```
    • 从上面的更新解决方案^^^,只需在获取数据并共享查询之前运行dd($queryBuilder->toSql());
    • 更新解决方案@DeekshaRana 立即尝试。 Edited 部分。
    猜你喜欢
    • 2023-01-14
    • 2021-12-26
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2021-12-11
    • 2020-10-11
    相关资源
    最近更新 更多