$events = $events->select('y_people_lib_id','location_id')->whereIn('location_id',$Inlocation)->where('created_at','>=',$date.' '.'00:00:01')->where('created_at','<=',$date.' '.'23:59:59')->orderBy('created_at', 'DESC')->groupBy('y_people_lib_id')->get()->toArray();
这是我现在写的 我想y_people_lib_id去重 并保留created_at最大的值
我能想到的是子查询,类似下面这样子,select * from (SELECT * FROM location_exception
ORDER BY date_time DESC) as b group by device_id;
用distinct函数可以去重。针对create_at字段进行降序排序order by,针对排序后的结果取第一条记录,这样取到的就是最大值了。如果不明白可以继续追问。
groupby 取max