如何在 Laravel 5.0 中使用外部全连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41662033/
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
How to use outer full join in laravel 5.0?
提问by Samuel Ricky
It's my controller :
这是我的控制器:
public function lihatpesanansemua() //ajax
{
if(Request::ajax())
{
$hasil = DB::table('pesanan')->join('pemesan','pemesan.id', '=', 'pesanan.idpemesan')->join('komputer', 'komputer.id' ,'=', 'pesanan.idkomputer')
->select('pesanan.id', 'pemesan.nama', 'pesanan.tglpesan', 'pesanan.jampesan', 'pesanan.jamakhir', 'komputer.nama_komputer', 'komputer.lantai', 'komputer.Kelas')
->orderby('pesanan.id', 'asc')
->get();
$hasil = json_encode($hasil);
return $hasil;
}
}
And that's is inner join. How to change to full outer join ? Thanks, sorry my bad english
这就是内连接。如何更改为全外连接?谢谢,对不起我的英语不好
回答by Tim Biegeleisen
I don't know what exactly your query is trying to achieve, and where you need a full outer join, but I will begin this answer by saying that MySQL has no inbuilt support for full outer join. Based on this SO question, we can find a way an alternative way to write the following query:
我不知道您的查询究竟想要实现什么,以及您需要完全外连接的位置,但是我将首先说 MySQL 没有对完全外连接的内置支持。基于这个 SO question,我们可以找到一种替代方法来编写以下查询:
SELECT * FROM t1
FULL OUTER JOIN t2
ON t1.id = t2.id
This can be rewritten as a UNION
of a left join and a right join:
这可以重写为UNION
左连接和右连接的 a :
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
In Laravel, we can write the following code to represent the above full outer join:
在 Laravel 中,我们可以编写以下代码来表示上述全外连接:
$second = DB::table('t2')
->rightJoin('t1', 't1.id', '=', 't2.id')
$first = DB::table('t1')
->leftJoin('t2', 't1.id', '=', 't2.id')
->unionAll($first)
->get();
Again, I don't know why you think you need two outer joins, but regardless you should be able to adapt the above code and query and use it for your situation.
同样,我不知道为什么您认为需要两个外部联接,但无论如何您都应该能够调整上述代码和查询并将其用于您的情况。
References:
参考:
- Link to full outer joins in MySQL: Full Outer Join in MySQL
- Link to Laravel 5.3 query builder docs: https://laravel.com/docs/5.3/