Database 不存在的Laravel不能正常工作
据我所知,whereNotExists应该排除传递的闭包中的所有查询。 然而,我得到了意想不到的结果 我想做的是返回所有申请关闭条件的学生;未被家长设定为缺席或缺席的学生。我得到的是一个空的students数组[] 我做错了什么Database 不存在的Laravel不能正常工作,database,postgresql,laravel,eloquent,Database,Postgresql,Laravel,Eloquent,据我所知,whereNotExists应该排除传递的闭包中的所有查询。 然而,我得到了意想不到的结果 我想做的是返回所有申请关闭条件的学生;未被家长设定为缺席或缺席的学生。我得到的是一个空的students数组[] 我做错了什么 $students = DB::table('rounds') ->where('rounds.bus_id', '=', $bus_id) ->join('rounds_student
$students = DB::table('rounds')
->where('rounds.bus_id', '=', $bus_id)
->join('rounds_students', 'rounds_students.round_id', 'rounds.id')
->whereNotExists(function ($query) {
$query->select(DB::raw(1))
->from('student_history')
->where('student_history.student_id', '=', 'rounds_students.student_id')
->where('student_history.activity_type', '=', 'absent')
->orWhere('student_history.activity_type', '=', 'absent-by-parent');
})
->join('students', 'students.id', 'rounds_students.student_id')
->select('students.name')
->get();
return $students;
whereRaw()
或where(DB::raw(“…”)
一起使用,才能在NOT EXISTS
子句中定义相关条件。否则,'rounds\u students.student\u id'
将作为字符串值传递,这不是您想要的和s和或s在您的WHERE
条件下!你的不正确。将其更改为,其中
,使其更加简洁
EXISTS
子句中使用select(DB::RAW(1))
。数据库优化器知道他们不需要将任何结果集返回到外部查询。这可能有助于减少代码膨胀
试一试
$students=DB::table('rounds')
->选择('students.name')
->加入('rounds\u students'、'rounds\u students.round\u id'、'rounds.id')
->加入('students','students.id','rounds\u students.student\u id'))
->其中('rounds.bus\u id',$bus\u id)
->whereNotExists(函数($query){
$query->from('student\u history')
->whereRaw('student\u history.student\u id=rounds\u students.student\u id')
->其中('student_history.activity_type',['缺席','家长缺席');
})
->get();
whereRaw()
或where(DB::raw(“…”)
一起使用,才能在NOT EXISTS
子句中定义相关条件。否则,'rounds\u students.student\u id'
将作为字符串值传递,这不是您想要的和s和或s在您的WHERE
条件下!你的不正确。将其更改为,其中
,使其更加简洁
EXISTS
子句中使用select(DB::RAW(1))
。数据库优化器知道他们不需要将任何结果集返回到外部查询。这可能有助于减少代码膨胀
试一试
$students=DB::table('rounds')
->选择('students.name')
->加入('rounds\u students'、'rounds\u students.round\u id'、'rounds.id')
->加入('students','students.id','rounds\u students.student\u id'))
->其中('rounds.bus\u id',$bus\u id)
->whereNotExists(函数($query){
$query->from('student\u history')
->whereRaw('student\u history.student\u id=rounds\u students.student\u id')
->其中('student_history.activity_type',['缺席','家长缺席');
})
->get();
通常不存在
子查询使用外部查询的某些元素。“在这里,你根本就没有这么做。”apokryfos我已经这么做了,但忘了在问题中提到它。用它更新了问题。我仍然得到一个空的$students数组。通常,不存在
子查询使用外部查询的某些元素。“在这里,你根本就没有这么做。”apokryfos我已经这么做了,但忘了在问题中提到它。用它更新了问题。我仍然有一个空的$students数组。