2017-09-15 57 views
0
$productLists = DB::table('A') 
        ->leftJoin('B', function($join) { 
         $join->where('B.qty','=', 1); 
         $join->on("B.id", "=", "A.id"); 
        }) 
        ->select('A.*','B.*') 
        ->get(); 

查询有什么问题?如何使用查询生成器laravel在leftjoin期间加入子查询?

我从B表中得到的字段全部返回null

我以错误的方式做了吗?

+1

我会把' - >在哪里(” B.qty','=',1)'left_oin'回调外 – aaron0207

回答

0

试试这个:

$productLists = DB::table('A') 
       ->leftJoin('B', function($join) { 
         $join->where('B.qty','=', 1) 
          ->on("B.id", "=", "A.id"); 
       }) 
       ->select('A.*','B.*') 
       ->get(); 
+0

只是发布代码不是一个好的答案。描述你做了什么以及为什么你认为它可以解决问题。 –

0
$productLists = DB::table('A') ->select('A.*','B.*')->join('B','B.id','=','A.id')->where('B.qty', 1) ->get(); 

这是写一种方式联接在laravel ..

0

试试这个:

$productLists = DB::table('A') 
        ->join('B', 'B.id', '=', 'A.id') 
        ->select('A.*','B.*') 
        ->where('B.qty','=', 1) 
        ->get();