有没有办法选择连接失败的所有地方:
类似的东西:
// Pseudo
$unit = User::from( 'users' )
->select( 'users.*' )
->whereJoinFails( 'suppliers', 'users.id', '=', 'suppliers.user_id' );
我试过了:
// Returns all users
$unit = User::from( 'users' )
->select( 'users.*' )
->leftJoin( 'suppliers', 'users.id', '=', 'suppliers.user_id' );
和
// Returns all users but the one that doesn't satisfy is duplicated
$unit = User::from( 'users' )
->select( 'users.*' )
->join( 'suppliers', 'users.id', '!=', 'suppliers.user_id' );
答案 0 :(得分:2)
Left Join将始终显示连接左侧的数据,即使它为null。这样,您可以查看供应商表中缺少的所有记录,因为它将显示为空。这应该有效:
User::from( 'users' )
->select( 'users.*' )
->leftJoin('suppliers', 'users.id', '=', 'suppliers.user_id')
->whereNull('suppliers.user_id')
->get();
答案 1 :(得分:0)
替代解决方案是:
$cat = User::whereNotIn('id',
DB::table('suppliers')->distinct()->lists('user_id')
)->get();
但在这种情况下,将执行2次对数据库的查询,例如:
select distinct `user_id` from `suppliers`;
select * from `users` where `id` not in ('1', '2', '5', '6', '7', '8', '9', '10');