如何在Laravel 5.0中使用外部完全连接? [英] How to use outer full join in laravel 5.0?

查看:89
本文介绍了如何在Laravel 5.0中使用外部完全连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的控制器:

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

推荐答案

我不知道您的查询到底想实现什么,以及您需要在何处进行完全外部联接,但是我将通过说MySQL来开始这个答案.没有对完全外部联接的内置支持.基于这个SO问题,我们可以找到一种替代方法来编写以下内容查询:

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:

SELECT * FROM t1
FULL OUTER JOIN t2
    ON t1.id = t2.id

这可以重写为左联接和右联接的UNION:

This can be rewritten as a UNION of a left join and a right join:

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id

在Laravel中,我们可以编写以下代码来表示上述完整的外部联接:

In Laravel, we can write the following code to represent the above full outer join:

$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.

参考:

  • 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/

这篇关于如何在Laravel 5.0中使用外部完全连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆