laravel 5.4 中的全外连接查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46115932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:37:45  来源:igfitidea点击:

Full outer join query in laravel 5.4

laravel

提问by VinoCoder

I have two table application_requestsand transactionsin both the tables there may be matching record or may not be. For any condition i want record from both the tables.

我在两个表中有两个表application_requests事务,可能有匹配的记录,也可能没有。对于任何条件,我都希望从两个表中记录。

In transactionstable there is a foreign key column application_request_id(this has value of reference of primary key in application_requeststable).. If this condition matched then it should display as one row (or record).

transactions表中有一个外键列application_request_id(这在application_requests表中有主键的引用值)。如果这个条件匹配,那么它应该显示为一行(或记录)。

I dont know how to achieve this in laravel.

我不知道如何在 Laravel 中实现这一点。

I have tried below codes but its not working:

我试过下面的代码,但它不工作:

    $a = \DB::table('application_requests')->select('id');

    $b  = \DB::table('transactions')->select('application_request_id');

    $results = $b->union($a)->get();

    echo "<pre>";
    print_r($results);die;

transactions table is enter image description here

交易表是 enter image description here

And my application_requests table is enter image description here

我的 application_requests 表是 enter image description here

回答by marijnz0r

Two outer joins to show all rows from both tables:

两个外连接显示两个表中的所有行:

$second = DB::table('t2')
             ->rightJoin('t1', 't1.id', '=', 't2.id')

$first = DB::table('t1')
            ->leftJoin('t2', 't1.id', '=', 't2.id')
            ->unionAll($second)
            ->get();

Source: https://stackoverflow.com/a/41662283/4587214

来源:https: //stackoverflow.com/a/41662283/4587214

回答by Maulik

    $results = DB::table('transactions')
                ->leftJoin('application_requests','transactions.application_request_id','=','application_requests.id')
                ->select('transactions.partner_id as tr_pratnerid','transactions.application_request_id as tr_applicationrequestid','transactions.class_2_1 as tr_clas21','transactions.class_2_2 as tr_clas22','transactions.class_2_3 as tr_clas23','transactions.class_3_1 as tr_clas31',
                 DO THIS TO ALL FIELDS FROM BOTH TABLE)
                ->get();
    echo "<pre>";
    print_r($results);die;

回答by Mild Academy

just like that:

就这样:

DB::table('transactions')->join('application_requests', 'transactions.application_request_id', '=', 'application_requests.id', 'full outer');