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
Full outer join query in laravel 5.4
提问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;
回答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();
回答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');