laravel 5.4中的子查询

时间:2018-06-20 07:53:46

标签: php mysql sql laravel laravel-5.4

如何查询查询生成器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) ;

2 个答案:

答案 0 :(得分:2)

为此使用左连接

$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();

答案 1 :(得分:0)

我想我解决了。 。

$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();