我需要以下数据中的所有更新行,我的表中有一个重复的托运id,从这个开始,我只需要更新每个装运id的行。
在我的表中有多个装运In,它们都是重复的,如下所示:
id date shipment_id
1 2020-09-09 12
2 2020-10-05 12
3 2020-10-23 12
4 2020-09-09 13
5 2020-10-05 13
6 2020-10-23 13产出应是:
id date shipment_id
3 2020-10-23 12
6 2020-10-23 13我需要每一个shipment_id的最新记录
发布于 2022-04-06 18:00:15
您可以这样做(使用最大值查询的组)
$data = Shipments::select('id', 'shipment_id', DB::raw('MAX(date) as latest_date'))
->where('is_active', 1)
->groupBy('shipment_id')
->get();
dd($data);参考文献:https://www.itsolutionstuff.com/post/laravel-group-by-with-max-value-query-exampleexample.html
发布于 2022-04-06 18:00:28
你应该看看通过雄辩的ORM查询。
查询应该如下所示:
Shipments::orderBy('id', 'desc') ->groupBy('shipment_id') ->get();为了加快查询速度,我们可以利用您的id(increment_id),因此最大的id (具有特定的shipement_id )总是最新的。
https://stackoverflow.com/questions/71771154
复制相似问题