Laravel MIN MAX GroupBy 来自集合
Posted
技术标签:
【中文标题】Laravel MIN MAX GroupBy 来自集合【英文标题】:Laravel MIN MAX GroupBy from Collection 【发布时间】:2021-06-22 09:50:14 【问题描述】:我有一个查询,然后我使用 forEach 循环。
我通过消除空值过滤然后推入一个集合
然后我将二维数组展平为一维数组。
所以,我有收集格式的数据。
我的问题是如何从集合中获取 MIN MAX?
例如我的数据:
Illuminate\Support\Collection #193
#items: array:8 [
0 => #388
+"id": 498
+"id_user": "20021635"
+"name": "Jeany"
+"department": "IAD"
+"location": "Rose Tower – 1st Floor"
+"updated_at": "2021-03-18 10:12:15"
1 => #379
+"id": 498
+"id_user": "20021635"
+"name": "Jeany"
+"department": "IAD"
+"location": "Rose Tower – 1st Floor"
+"updated_at": "2021-03-18 10:25:16"
2 => #394
+"id": 498
+"id_user": "20021635"
+"name": "Jeany"
+"department": "IAD"
+"location": "Rose Tower – 1st Floor"
+"updated_at": "2021-03-18 10:29:16"
3 => #395
+"id": 498
+"id_user": "20021635"
+"name": "Jeany"
+"department": "IAD"
+"location": "Rose Tower – 1st Floor"
+"updated_at": "2021-03-18 10:05:15"
4 => #389
+"id": 470
+"id_user": "10025488"
+"name": "Michael Angelo"
+"department": "HR"
+"location": "Rose Tower – 1st Floor"
+"updated_at": "2021-03-18 10:07:15"
5 => #397
+"id": 470
+"id_user": "10025488"
+"name": "Michael Angelo"
+"department": "HR"
+"location": "Rose Tower – 1st Floor"
+"updated_at": "2021-03-18 10:09:15"
6 => #401
+"id": 470
+"id_user": "10025488"
+"name": "Michael Angelo"
+"department": "HR"
+"location": "Rose Tower – 1st Floor"
+"updated_at": "2021-03-18 10:12:15"
7 => #402
+"id": 470
+"id_user": "10025488"
+"name": "Michael Angelo"
+"department": "Rose Tower"
+"location": "Rose Tower – 1st Floor"
+"updated_at": "2021-03-18 10:14:15"
]
预期输出:
+"id": 498
+"id_user": "20021635"
+"name": "Jeany"
+"department": "IAD"
+"location": "Rose Tower – 1st Floor"
+"time_in": "2021-03-18 10:05:15"
+"time_out": "2021-03-18 10:29:16"
+"id": 470
+"id_user": "10025488"
+"name": "Michael Angelo"
+"department": "HR"
+"location": "Rose Tower – 1st Floor"
+"time_in": "2021-03-18 10:07:15"
+"time_out": "2021-03-18 10:14:15"
代码:
$person = collect([]);
foreach ($machine_list as $key => $value)
$contactBy = DB::tamachine('attendance')
->select(DB::raw('user.id as id,
user.idemployee as id_user,
user.name as name,
user.id_dept as department,
machine.loc_name as location,
attendance.updated_at'))
->Join('user','attendance.id_user','=','user.id')
->Join('machine','attendance.id_machine','=','machine.id_machine')
->where(function ($query) use ($value)
$query->where('attendance.id_machine', $value->id_machine)
->orWhere('attendance.id_machine2', $value->id_machine2)
->orWhere('attendance.id_machine3', $value->id_machine3);
)
->where('attendance.id_user','<>',$id_user)
->whereBetween('attendance.updated_at', [Carbon::parse($minmax_person[0]->timestart,'GMT'), Carbon::parse($minmax_person[0]->timeend,'GMT')])
// ->groupBy('user.id','user.idemployee','user.name','user.id_dept','machine.loc_name')
->get();
if(!$contactBy->isEmpty())
$person->push($contactBy);
$person->flatten();
任何帮助将不胜感激
【问题讨论】:
展示你是如何得到这个脚本的,而不仅仅是解释它。这样就可以派生出您正在使用的工具。 完成@AntonyMN。 【参考方案1】:Laravel Collection 方法
创建一个空集合对象,然后在现有集合对象上使用 min 和 max 方法,并使用 push 方法将其值推送到空集合对象中
Max
Min
Push
【讨论】:
以上是关于Laravel MIN MAX GroupBy 来自集合的主要内容,如果未能解决你的问题,请参考以下文章
Django 查询模型 - GROUP BY、MIN、MAX
MybatisPlus Lambda表达式 聚合查询 分组查询 COUNT SUM AVG MIN MAX GroupBy
使用 where min/max laravel 获取所有行