连接中的Laravel限制

时间:2018-07-18 05:50:38

标签: mysql laravel laravel-5 greatest-n-per-group

我的一个控制器中具有以下功能:

$documentReminders = DB::table('employees')
                ->where('department_id',5)
                ->join('employee_attachments', 'employees.id', '=', 'employee_attachments.employeeID')
                    ->whereIn('category', ['Medical Examiners Certificate','Drivers License'] )
                ->orderBy('employee_attachments.endDate')
                ->get();

这确实也成功返回了我期望的所有记录,但是这里的问题是太多了(我会解释)。

对于每条员工记录,employee_attachments.category中的每一个可能有几十个(例如,一名雇员可能有8名,而一名雇员可能有15名)。

我希望能够将每个雇员返回的employee_attachments的数量限制为每个类别的一(1),并且该类别是该类别中关于其{{1 }}字段。

基本上,如果我有以下employee_attachments.endDate

employee_attachments

然后我要返回以下内容:

employeeID     || category                      || endDate
1              || Drivers License               || 2019-01-01
1              || Drivers License               || 2017-01-01
2              || Drivers License               || 2016-01-01
1              || Drivers License               || 2018-01-01
1              || Medical Examiners Certificate || 2017-01-01
1              || Medical Examiners Certificate || 2018-01-01
1              || Medical Examiners Certificate || 2019-01-01
2              || Medical Examiners Certificate || 2017-01-01
2              || Medical Examiners Certificate || 2020-01-01

2 个答案:

答案 0 :(得分:1)

这是一种简单的方法。

 $documentReminders = DB::table('employees')
        ->join('employee_attachments', 'employees.id', '=', 'employee_attachments.employeeID')
        ->select(['employee_attachments.employeeID','employee_attachments.category',DB::raw('MAX(employee_attachments.endDate) as eDate')])
            ->where('department_id',5)
    ->whereIn('category', ['Medical Examiners Certificate','Drivers License'] )
    ->groupBy('employee_attachments.employeeID','employee_attachments.category')
    ->orderBy('eDate','DESC')
    ->get();

答案 1 :(得分:0)

要从employee_attachments获取每位员工的最新记录,您可以使用employee_attachments表引入自我联接,并附加endDate应该最高的附加联接条件

$documentReminders = DB::table('employees as e')
                        ->where('department_id',5)
                        ->join('employee_attachments as ea', 'e.id', '=', 'ea.employeeID')
                        ->leftJoin('employee_attachments as ea1', function ($join) {
                            $join->on('ea.employeeID', '=', 'ea1.employeeID')
                                 ->where('ea.category', '=', 'ea1.category')
                                 ->where('ea.endDate', '<', 'ea1.endDate');
                        })
                        ->whereNull('ea1.employeeID')
                        ->whereIn('ea.category', ['Medical Examiners Certificate','Drivers License'] )
                        ->orderBy('ea.endDate')
                        ->select('e.*','ea.*')
                        ->get();    

Laravel Eloquent select all rows with max created_at

Laravel - Get the last entry of each UID type

Laravel Eloquent group by most recent record

在原始sql中,将是

select ea.*
from employee_attachments ea
left join employee_attachments ea1 
     on ea.employeeID = ea1.employeeID
     and ea.category = ea1.category
     and ea.endDate < ea1.endDate
where ea1.employeeID is null
order by ea.employeeID

Demo