How to get the last item in a group in the query builder, if not an integer or date?

I am trying to get the last line using groupby, I have a duplicate jobseeker_id, but the difference is call_reason, status, type_of_call etc ... and created_at.

id jobseeker_id  type_of_call   status  call_reason        created_at
1   2001          incoming_call  good   because of track   2017.10.1
2   2001          outgoing call  fair    they called me    2017.11.2
3   2001          outgoing call  bad     something         2017.12.3
4   2002          outgoing call  good     something        2018.11.6
Run codeHide result

So, I expected to go out only

id jobseeker_id  type_of_call   status  call_reason        created_at
1   2001         outgoing call  bad      something        2017.12.3
4   2002          outgoing call  good     something        2018.11.6
Run codeHide result
Here is my request

           $jobseekers =DB::table('calllogs')
  ->select(DB::raw("max(created_at),jobseeker_id,call_reason,type_of_call"))
    ->orderBy('created_at','desc')
  ->groupBy('jobseeker_id')  
  ->wherenotnull('call_back_date')
  ->get();
Run codeHide result

the above query returns me as

id jobseeker_id  type_of_call   status  call_reason        created_at
3   2001          incoming_call  good     because of track         2017.12.3
4   2002          outgoing call  good     something        2018.11.6
Run codeHide result

I can only get the latest created_at date, but I can get the latest staus, type_of_call, call_reason.Can anyone have an idea? please help me.

+4
source share
2 answers

laravel, , , . max created_at, . .

SELECT * FROM calllogs WHERE id IN (
(SELECT MAX(ID) FROM calllogs GROUP BY jobseeker_id);

, , , 3, 1. , , , . , , , , . .

+1
$jobseekers =DB::table('calllogs')
  ->select(DB::raw("max(created_at),jobseeker_id,call_reason,type_of_call"))
  ->orderBy('jobseeker_id','desc')
  ->wherenotnull('call_back_date')
  ->take(2)
  ->get();
0

Source: https://habr.com/ru/post/1690985/


All Articles