我明白了:
SELECT deals.* FROM deals
JOIN deals_bookings ON (deals_bookings.deal_id = deals.ID)
WHERE deals_bookings.date > CURDATE()
ORDER BY deals_bookings.date DESC
在此,我想排除当前有效的交易(有Deal_bookings.date = CURDATE())
因此,如果我们在deals_bookings中假装这些行:
deal_id 2 date 2012-02-05
deal_id 3 date 2012-02-05
deal_id 2 date 2012-02-06
deal_id 4 date 2012-02-06
这应该只返回deal_id 4行,因为它大于CURDATE()并且当前不活动(今天的日期是2012-02-05)。
目前它返回deal_id 2和deal_id 4,因为我没有提及排除那些当前处于活动状态的人(有一行curdate())
我该怎么做?
答案 0 :(得分:0)
你可以这样做:
select d.*
from deals d
join deals_bookings b on d.id = b.deal_id
where b.date > curdate()
and b.deal_id not in (
select deal_id
from deals_bookings
where date <= curdate()
)
order by b.date desc
如果他们在今天或之前有一个日期条目,那么这将为您提供现有资源,但删除了deals_bookings
个值。