如何在Laravel中的MIN和MAX之间进行搜索

时间:2018-10-03 10:51:38

标签: laravel laravel-5 laravel-5.2 laravel-5.1

我知道在MySQL中查询看起来像这样...

SELECT id 
    FROM listings 
    WHERE id  IN (
      SELECT id
      FROM listings
      WHERE price between 200 and 500
    );

在Laravel查询中,我尝试过...

Listing::select('listings.*')
    ->whereBetween('price', [200, 500])
    ->groupBy('listings.id')
    ->orderBy('listings.id', 'desc')
    ->paginate(1000);

它只显示一个结果。我在哪里出错了?谢谢您的帮助!

2 个答案:

答案 0 :(得分:4)

尝试

Listing:: where(price, '>=', 200 )
                 ->where(price, '<=', 500 )
                 ->groupBy('id')
                 ->orderBy('id', 'desc')
                 ->paginate(1000);

答案 1 :(得分:2)

Listing::select('listings.*')
                 ->whereRaw("(listings.price <= ? AND listings.price >= ?) ", [500, 200])
                 ->groupBy('listings.id')
                 ->orderBy('listings.id', 'desc')
                 ->paginate(1000);