Sql leftJoin无法获取左表中的所有行

Sql leftJoin无法获取左表中的所有行,sql,laravel-4,eloquent,Sql,Laravel 4,Eloquent,我有以下疑问 #Query 1 $count = User::where('users.status', '=', 1) ->leftJoin('deposits', 'users.id', '=', 'deposits.user_id') ->select('users.*') ->groupBy('deposits.user_id') ->count(); # Count == 71 # Query 2 User::where('

我有以下疑问

#Query 1
$count = User::where('users.status', '=', 1)
    ->leftJoin('deposits', 'users.id', '=', 'deposits.user_id')
    ->select('users.*')
    ->groupBy('deposits.user_id')
    ->count();

# Count == 71


# Query 2
User::where('users.status', '=', 1)->count()

# Count == 89
$resellers = User::where('users.status', '=', UserStatus::getUserStatusAsInteger('reseller') )
                    ->leftJoin('deposits', 'users.id', '=', 'deposits.user_id')
                    ->where('deposits.status', '=', 'completed')
                    ->where('deposits.created_at', '>=',  \Carbon\Carbon::now()->subDays(25) )
                    ->having( DB::raw( 'sum( deposits.amount )') , '<', Settings::first()->min_reseller_deposit )
                    ->select('users.*',  DB::raw( 'sum( deposits.amount ) as `total_deposits`' ))
                    ->groupBy('users.id')
                    ->get();
请问为什么我没有得到用户表中的所有行

最后,我将运行以下查询

#Query 1
$count = User::where('users.status', '=', 1)
    ->leftJoin('deposits', 'users.id', '=', 'deposits.user_id')
    ->select('users.*')
    ->groupBy('deposits.user_id')
    ->count();

# Count == 71


# Query 2
User::where('users.status', '=', 1)->count()

# Count == 89
$resellers = User::where('users.status', '=', UserStatus::getUserStatusAsInteger('reseller') )
                    ->leftJoin('deposits', 'users.id', '=', 'deposits.user_id')
                    ->where('deposits.status', '=', 'completed')
                    ->where('deposits.created_at', '>=',  \Carbon\Carbon::now()->subDays(25) )
                    ->having( DB::raw( 'sum( deposits.amount )') , '<', Settings::first()->min_reseller_deposit )
                    ->select('users.*',  DB::raw( 'sum( deposits.amount ) as `total_deposits`' ))
                    ->groupBy('users.id')
                    ->get();
$resellers=User::where('users.status','=',UserStatus::getUserStatusAsInteger('reseller'))
->leftJoin('deposits','users.id','=','deposits.user_id'))
->其中('deposits.status'、'='、'completed')
->其中('depositions.created_at','>=',\Carbon\Carbon::now()->subDays(25))

->具有(DB::原始('sum(depositions.amount)'),“即使您在表上执行
左联接
操作,您仍然使用
联接
条件,并且查询将只返回满足该条件的行。由于
表中没有与该条件不匹配的行,因此不会显示
null
值。

在第一次查询中在上,您无法取回所有行是因为您按存款进行分组。用户id不适用于所有用户

->groupBy('deposits.user_id')
相反,您应该按
users.id

在第二个查询中,您没有获得左表中的所有行的原因是因为您在
where
子句中引用了右表
depositions
,这意味着无论何时
depositions
上没有匹配项,这些值都将为null,并且
where
将失败

解决方案是将
存款的
where
条件移动到您的
左连接中

leftJoin('deposits', function($join) {
    $join->on('deposits.user_id', '=', 'users.id')
         ->on('deposits.status','=',DB::raw("'completed'"))
         ->on('deposits.created_at', '>=',  \Carbon\Carbon::now()->subDays(25);
})

请你帮我做另一个查询,把所有用户连同他们的存款放在一起。如果他们有存款?我不确定。你为什么在你的对账单中使用
分组依据
。你能试着去掉它吗?如果我去掉它,我会得到重复的行,因为一个用户可以有超过1笔存款。根据你说的,有一个用户表中有89行的状态为
1
。这就是您试图得到的结果吗?我仍然得到一个错误。这个叠加流注释标记非常有限。您可以在这里@beclient see update看到错误,您需要使用
DB::raw
所以
completed
被解释为字符串而不是列