laravel5.4中的php子查询

csbfibhn  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(290)

如何对查询生成器laravel 5.4进行此查询?

select * from gb_employee where employee_id not in (select gb_emp_client_empid from gb_emp_client_lines where gb_emp_client_clientid =1) ;
x759pob2

x759pob21#

我想我解决了。

$employees = DB::table('gb_employee')
                            ->whereNotIn('employee_id', function($query) use ($client_id)
                            {
                                $query->select('gb_emp_client_empid')
                                      ->from('gb_emp_client_lines')
                                      ->where('gb_emp_client_clientid',$client_id);
                            })
                            ->get();
oxf4rvwz

oxf4rvwz2#

为此使用左连接

$employee=DB::table('gb_employee as e')
                ->select('e.*')
                ->leftJoin('gb_emp_client_lines as el', 'e.employee_id', '=', 'el.gb_emp_client_empid')
                ->whereNull('el.gb_emp_client_empid',)
                ->get()
;

口才

class Employee extends Model
{

    public function client_lines()
    {
        return $this->hasMany('App\ClientLines', 'gb_emp_client_empid', 'employee_id');
    }
}

$employees = Employee::doesntHave('client_lines')->get();

相关问题