是否可以在laravel中连接下表?

lzfw57am  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(296)

如何在laravel中连接两个具有多个列的表,如下面的查询:

SELECT
    *
FROM
    tab1 AS e
INNER JOIN
    tab2 AS v ON v.Case1 = e.Case1 and v.id = e.id;

我在谷歌上搜索过,但找不到任何方法。
这是我迄今为止尝试的代码:

DB::table('tab1 as e')
    ->join('tab2 as v', 'v.Case1', '=', 'e.Case1', 'and', 'v.id', '=', 'e.id')
    ->get();
ifsvaxew

ifsvaxew1#

这就是你必须绕过它的方式,它必须起作用。就拿我的这个例子来说,我做到了,而且很管用

$categ =  DB::connection('mysql')->table('SubCategories')
                 ->leftJoin('Categories', 'Categories.id', '=', 'SubCategories.categories_id')
            ->select('SubCategories.id','SubCategories.Code','SubCategories.Name','SubCategories.Description','Categories.Name as CategoryName'
              )
            ->get();

DB::connection('your connection from app.php file')table('tab1 as e')
    ->join('tab2 as v', 'v.Case1', '=', 'e.Case1', 'and', 'v.id', '=', 'e.id')
    ->Select('your select statement'),
    ->get();
im9ewurl

im9ewurl2#

你试过以下方法吗?

DB::table('tab1 as e')
->join('tab2 as v', function($join)
{
   $join->on('v.Case1', '=', 'e.Case1');
   $join->on('v.id','=', 'e.id');

})
->get();

相关问题