请试试这个:
SELECT reservation_property_id, MIN(reservation_date)
FROM (SELECT IF(@prevProperty != q.reservation_property_id,
@runningtotal:=0,
@runningtotal:=@runningtotal + payment_amount) AS runningtotal,
@prevProperty := q.reservation_property_id, q.*
FROM (SELECT *
FROM reservations r
INNER JOIN payments p
ON p.payment_reservation_id = r.reservation_id
/*not sure how your tables are linked*/
, (SELECT @runningtotal:=0, @prevProperty:=0) vars
ORDER BY r.reservation_property_id, reservation_date) q
) sq
WHERE runningtotal >= 500
GROUP BY reservation_property_id
不过,出于方便,我没有加入 Properties 表 :)
这个想法基本上只是计算按属性和日期排序的表格上的总付款。在外部查询中,只需选择运行总计等于或大于阈值的最小日期。