我有两个类似的表条目,如下所示:
gym_clients
+---------+---------+-----------------+
| id | first_name| last_name |
+---------+-----------+---------------+
| 1 | Name 1 | Last 1 |
| 2 | Name 2 | Last 2 |
+---------+-----------+---------------+
gym_client_purchases
+---------+---------+-----------------+
| id | client_id | purchase_date |
+---------+-----------+---------------+
| 15 | 1 | 2018-04-01 |
| 16 | 1 | 2018-05-01 |
| 17 | 2 | 2018-05-01 |
+---------+-----------+---------------+
我想按每个客户的购买分组并显示最新的。
我试过这个查询,但 groupby 显示了我最旧的购买(ID 15)。
有任何想法吗?
$this->data['latestSubscriptions'] = GymPurchase::select('first_name', 'last_name', 'gym_client_purchases.*')
->leftJoin('gym_clients', 'gym_clients.id', '=', 'client_id')
->groupBy('gym_client_purchases.client_id')
->orderBy('gym_client_purchases.purchase_date', 'desc')
->get();
提前致谢。
鸿蒙传说