我有两个表:auctions
和bids
。在auctions
,我有一个start_amount
字段,bids
,amount
。这两个表还包含created_at
和modified_at
个时间戳列。我希望能够在一定范围内检索与最高(或最新,但是您看一下)出价匹配的拍卖列表。
过去一周我一直在使用它,并且只设法根据所有出价进行过滤,而不是最新(或最低)出价(start_amount
包含在内。)我会实现我想要的吗?
我尝试过的代码:
if (request()->has('bid_range'))
$auctions->with(['bids' => function ($query) use ($bid_min, $bid_max) {
$query->whereBetween('amount', [$bid_min, $bid_max])->orWhereBetween('start_amount', [$bid_min, $bid_max])->orderBy('created_at', 'desc')->get();
}]);
答案 0 :(得分:1)
如果我错了,请纠正我,但你可能会尝试这样做:
select *
from auctions
where exists
(select 1
from bids
where amount > 10 and amount < 20
and bids.auctions_id = auction.id)
所以,Laravel方式:
DB::table('auctions')
->whereExists(function ($query) use ($bid_min, $bid_max) {
$query->from('bids')
->whereRaw("'amount' > $bid_min and 'amount' < $bid_max")
->whereRaw('bids.auctions_id = auction.id');
})
->get();
这就是你想要的?