简体   繁体   English

Laravel - 从一个表中获取记录,该记录在另一个表中不存在,并附有 where 子句

[英]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 tableenrollments 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.

相关问题 使用laravel查询生成器并从一个表中查找另一个表中不存在的记录 - Query Builder and find records from one table which don't exist in another using laravel Laravel 5 where子句来自另一个表 - Laravel 5 where clause from another table 从一个表中选择 Laravel 5.1 中另一个表中不存在的所有记录 - Select all records from one table that do not exist in another table in Laravel 5.1 如何在 laravel 的另一个表中获取数据 where 子句 - how to get data where clause in another table in laravel 从一个表中仅选择另一个表中存在的记录 - Selecting only records from one table that exist in another table 如何从另一个表中没有特定值的UID的表中选择*? - How can I select * from a table where a UID doesn't exist with a certain value in another table? 选择Laravel关系表中不存在的记录 - Select records where not exist in relational table in laravel 将数据从一个表复制到另一个表,但是添加第二个WHERE子句不会插入数据 - Copy data from one table to another but adding 2nd WHERE clause won't insert data 使用where子句mysql将数据从一个表传输到另一个表 - Transferring data from one table to another using a where clause mysql WHERE子句与Laravel中的另一个表有关系 - WHERE clause with relationship to another table in Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM