有人可以帮我转换下面的mysql到laravel db query builder或eloquent吗?这是我的mysql
SELECT max(created_at), jobseeker_id,call_reason,type_of_call
FROM calllogs
GROUP BY jobseeker_id
ORDER BY created_at DESC;
这是我的尝试,但还没有运气。
$jobseekers =DB::table('calllogs')
->select(DB::raw("max('created_at'),jobseeker_id"))
->groupBy('jobseeker_id')
->orderBy('created_at','desc')
->get();
dd($jobseekers);
请帮帮我。
答案 0 :(得分:2)
$jobseekers =DB::table('calllogs')
->select(DB::raw("max(created_at),jobseeker_id"))
->groupBy('jobseeker_id')
->orderBy('created_at','desc')
->get();
尝试使用此代码。 只删除最多的'' 它会运作良好。
答案 1 :(得分:1)
DB::table(calllogs)
->select(DB::raw('max(created_at) as max_created_at'),
'jobseeker_id','call_reason','type_of_call')
->groupBy('jobseeker_id')
->orderBy('max_created_at', 'DESC')
->get(); =>As a array.
->first(); => As a object.
->join('table_1','table_1.id','=','table_2.table_1_id')
->leftjoin('table_1','table_1.id','=','table_2.table_1_id')
->rightjoin('table_1','table_1.id','=','table_2.table_1_id')
答案 2 :(得分:0)
ORDER BY子句不在GROUP BY子句中,并且包含非聚合列'myapp.calllogs.created_at'
该错误的两个解决方案
1.在config / database.php文件中,将sql strict模式设为false。
(或) 2.在groupBy Clause中添加所有表格列。
示例:
- > GROUPBY( 'jobseeker_id', 'jobseeker_name', 'jobseeker_email')