【发布时间】:2020-04-06 18:06:58
【问题描述】:
社区新人,向所有贡献者表示感谢!
现在我需要一个解决方案来从表中获取最新和不同的值。
我拥有的表名为“transactions”,其中包含'id'、'value'、'remarks'、'memberId(FK)'、'created_at' .
ID | amount | memberId | created_at
1 | 101 | 00001 | 22-04-2018
2 | 102 | 00002 | 22-04-2018
3 | 103 | 00002 | 22-04-2018
4 | 104 | 00001 | 24-04-2018 (latest)
5 | 105 | 00002 | 25-04-2018 (latest)
6 | 106 | 00003 | 25-04-2018 (latest)
我的预期结果是
ID | amount | memberId | created_at
4 | 104 | 00001 | 24-04-2018
5 | 105 | 00002 | 25-04-2018
6 | 106 | 00003 | 25-04-2018
我的代码
$transactions = Transaction::select('id', 'memberId', 'amount', DB::raw('max(created_at) as latest_depo'), 'created_at', 'amount')
->groupBy('memberId')
->orderBy('latest_depo' , 'desc')
->paginate(50);
但我最终没有得到每个成员的最新行。我的猜测与值的分组有关。提前致谢!
【问题讨论】: