问题
how can I join two tables with multiple columns in laravel, somthing like the below query:
SELECT
*
FROM
tab1 AS e
INNER JOIN
tab2 AS v ON v.Case1 = e.Case1 and v.id = e.id;
I have searched in google but could not find any way.
This is the code I've tried so far:
DB::table('tab1 as e')
->join('tab2 as v', 'v.Case1', '=', 'e.Case1', 'and', 'v.id', '=', 'e.id')
->get();
回答1:
Have you tried the following?
DB::table('tab1 as e')
->join('tab2 as v', function($join)
{
$join->on('v.Case1', '=', 'e.Case1');
$join->on('v.id','=', 'e.id');
})
->get();
回答2:
This is how you have to go around it and it must work. Take this example of mine I did and it works
$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();
来源:https://stackoverflow.com/questions/53457659/is-it-possible-to-join-table-as-below-in-laravel