Laravel: 对 Left Join 中的列进行排序
Laravel: Sorting a column in the Left Join
假设我有一个简单的 table 用户
id | userName
3 Michael
4 Mike
5 George
另外 table 他们的车和价格
id | belongsToUser | carPrice
1 4 5000
2 4 6000
3 4 8000
我想做一个左连接 return 最高或最低的 carPrice
目前,查询将 return 该用户 carPrice 的 last/first 实例。
我试过在各种连接查询中输入 orderBy 但无济于事。
我有一个辅助函数可以 return 按需 highest/lowest 价格,但我不确定它如何适合此查询,因为我想使用 laravels 内置分页
这是聚合问题,所以这是求解:
DB::table('users')
->leftJoin('carPrices', 'belongsToUser', '=', 'users.id')
->select('users.*', DB::raw('MAX(carPrice) as highestCarPrice'), DB::raw('MIN(carPrice) as lowestCarPrice'))
->groupBy('users.id')
->get();
假设我有一个简单的 table 用户
id | userName
3 Michael
4 Mike
5 George
另外 table 他们的车和价格
id | belongsToUser | carPrice
1 4 5000
2 4 6000
3 4 8000
我想做一个左连接 return 最高或最低的 carPrice 目前,查询将 return 该用户 carPrice 的 last/first 实例。 我试过在各种连接查询中输入 orderBy 但无济于事。
我有一个辅助函数可以 return 按需 highest/lowest 价格,但我不确定它如何适合此查询,因为我想使用 laravels 内置分页
这是聚合问题,所以这是求解:
DB::table('users')
->leftJoin('carPrices', 'belongsToUser', '=', 'users.id')
->select('users.*', DB::raw('MAX(carPrice) as highestCarPrice'), DB::raw('MIN(carPrice) as lowestCarPrice'))
->groupBy('users.id')
->get();