[英]How to get max value + other column using Laravel Eloquent
這是我的查詢:
$maxvalue= market::whereBetween('created_at', [$from, $to])->max('price');
但是除了max
外,我還想獲取user_id
列。 我該怎么做?
您將無法使用Eloquent的max()
函數,但可以將MySQL的max()
函數與DB::raw()
。 您還需要使用groupBy,以便每個用戶都可以使用它:
$maxValues = market::whereBetween('created_at', [$from, $to])
->groupBy('user_id')
->get(['user_id', DB::raw('max(price) as max_price'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.