这是我从MySQL的查询。我想使用' LEFT JOIN'两个相同的表,具有不同的别名,并有一个' ON'有' AND'括号内。这个查询到达的结果是正确的,但我的问题是如何才能知道这个查询与Laravel查询的等价?
SELECT * FROM table1 AS a
LEFT JOIN table1 AS b
ON (a.column1 = b.column1 AND a.column2 < b.column2)
WHERE b.column2 is null;
然后我在Laravel Query Builder中使用此查询,该查询将查询错误的结果。
DB::table('table1 As a')
->leftJoin('table1 As b', 'a.column1', '=', 'b.column1')
->leftJoin('table2 As c','a.column2', '<', 'c.column2')
->whereNull('c.column2')
->get();
这个Laravel查询等同于这个MySQL查询,不幸的是,它会查询相同的错误结果:
SELECT * FROM table1 AS a
LEFT JOIN table1 AS b
ON (a.column1 = b.column1 AND )
LEFT JOIN table1 AS c
ON (a.column2 < c.column2)
WHERE b.column2 is null;
答案 0 :(得分:0)
你需要将一个函数传递给DB::table('table1 as a')
->leftJoin('table1 as b', function($join) {
return $join->on('a.column1', '=', 'b.column1')
->whereColumn('a.column2', '<', 'b.column2');
})->get();
像这样的东西......
x = [5,1,2,3,4,5]
set(x)