Laravel 查询生成器不在

Laravel Query Builder WHERE NOT IN

我有以下 sql 查询

SELECT * FROM exams WHERE exams.id NOT IN (SELECT examId FROM testresults)

如何将其转换为 Laravel 查询生成器格式?

谢谢。

您可以将 whereNotIn 与闭包一起使用:

$result = DB::table('exams')->whereNotIn('id', function($q){
    $q->select('examId')->from('testresults');
})->get();

和Eloquent:

$result = Exams::whereNotIn('id', function($q){
        $q->select('examId')->from('testresults');
    })->get();