I'm performing this query with Laravel's query builder with a left join with the two tables linking on the column s_nid
. With the table I'm left joining to conference_report
there are many rows with one s_nid
and I only want to get back the row with the most recent date. I have been looking at Groupwise Maximum with some examples here on SO which is new to me and I think is what I need to complete the query but I'm unsure how to implement it on the table I'm joining to.
$data['students'] = DB::table('educational_report')
->select('educational_report.s_nid as s_nid'
,'educational_report.name as name'
,'educational_report.file_no as file_no'
,'educational_report.university as university'
,'conference_report.conference_end_date as conference_end_date'
,'student_status.status as status')
->leftJoin('student_status', 'student_status.s_nid', '=', 'educational_report.s_nid')
->leftJoin('conference_report', 'conference_report.s_nid', '=', 'educational_report.s_nid')
->distinct()
->groupBy('s_nid')
->paginate(10);
Possibly useful links:
Performing a Groupwise Maximum in Laravel 4
http://dev.mysql.com/doc/refman/5.6/en/example-maximum-column-group-row.html