[英]Laravel - Get records from one table that doesn't exist in another with a where clause attached
I've got the following SQL tables (in MySQL):我有以下 SQL 表(在 MySQL 中):
students
+-----+------------+
| id | first_name |
+-----+------------+
| 01 | John |
+-----+------------+
| 02 | Jane |
+-----+------------+
| 03 | Peter |
+-----+------------+
academics
+-----+-----------+----------+------+
| id | year_start| year_end |status|
+-----+-----------+----------+------+
| 10 | 2016 | 2017 |1 |
+-----+-----------+----------+------+
| 20 | 2017 | 2018 |0 |
+-----+-----------+----------+------+
enrollments
+----+------------+-------------+
| id | student_id | academic_id |
+----+------------+-------------+
| 1 | 01 | 10 |
+----+------------+-------------+
| 2 | 02 | 20 |
+----+------------+-------------+
| 3 | 01 | 20 |
+----+------------+-------------+
How do I get students from both the students table
and the enrollments table
who are not enrolled for the current academic year or of whom no records exists in the enrollments table
for the current academic year.如何获得来自两个学生
students table
和enrollments table
谁没有报名参加本学年或谁没有记录存在于的enrollments table
当前学年。
For now I can get the student form the students table
who has no enrollment details in the enrollments table
with this query:现在,我可以通过以下查询从
students table
获取在注册表中没有注册详细信息的enrollments table
:
$students = \DB::table('students')
->select(
'students.id',
'first_name'
)
->leftJoin('enrollments','enrollments.student_id','=','students.id')
->whereNull('enrollments.student_id')
->get();
Based on the data in the table above this query will return student Peter - 03
.根据上表中的数据,此查询将返回学生
Peter - 03
。
BUT HOW CAN I GET STUDENTS WHO HAVE NO ENROLLMENT DETAILS FOR THE CURRENT ACADEMIC YEAR?但是,我如何才能获得没有当前学年注册详细信息的学生?
This is how I determine the current academic year:这就是我确定当前学年的方式:
$current_academic = Academic::where('status', 1)->first();
With the existing query how do I join on the academics table
so that I can query out students who have no enrollment records for the current academic year.使用现有查询如何加入
academics table
以便查询当前学年没有注册记录的学生。 Will appreciate your earnest answers and suggestions.将感谢您的认真答复和建议。
$current_academic = Academic::where('status', 1)->first();
$students = \DB::table('students')
->select(
'students.id',
'first_name'
)
->whereNotExists( function ($query) use ($current_academic) {
$query->select(DB::raw(1))
->from('enrollments')
->whereRaw('students.id = enrollments.student_id')
->where('enrollments.academic_id', '=', $current_academic->id);
})
->get();
Let's give some details:让我们提供一些细节:
1- whereNotExists
clause will return only students that doesn't have any row in the sub query. 1-
whereNotExists
子句将仅返回在子查询中没有任何行的学生。
2- the sub query select students that exists in enrollments table and their academics_id is 10 2- 子查询选择存在于注册表中的学生,他们的 Academics_id 为 10
Hope this helps希望这可以帮助
$students = \DB::table('students')
->select(
'students.id',
'first_name'
)
->leftJoin('enrollments','enrollments.student_id','=','students.id')
->whereNull('enrollments.student_id')
->orWhere('enrollments.academic_id','<>',$current_academic->id)
->get();
Let's do this the most eloquential way possible:让我们以最有说服力的方式做到这一点:
First off, you need to have this in your Student.php model file首先,你需要在你的 Student.php 模型文件中有这个
public function academics()
{
return $this->belongsToMany('App\Academic', 'enrollments', 'student_id', 'academic_id');
}
and this in your Academic.php model file这在你的 Academic.php 模型文件中
public function students()
{
return $this->belongsToMany('App\Student', 'enrollments', 'academic_id','student_id');
}
now you can get what you want this way:现在你可以通过这种方式得到你想要的:
$students = \App\Student::whereDoesntHave('academics')
->orWhereHas('academics',function($q) use ($academic){
$q->where('id',$academic->id)->count();
},'=',0)->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.