我想檢查一個預訂ID = 2和booking_state = PENDING,如果有另一個預訂PENDING爲相同的boat_id和相同的日期。我唯一的問題是我想排除我的最終結果中的booking_id = 2。Mysql SQL減號查詢
SELECT distinct b1.id
FROM bookings b1, bookings b2, booking_states bs
WHERE bs.name = 'Pending'
AND b1.id = 2
AND b1.id <> b2.id
AND bs.id = b1.booking_state_id
AND bs.id = b2.booking_state_id
AND b1.boat_id = b2.boat_id
AND (date(b1.departure_date) <= b2.departure_date AND date(b1.arrival_date) >= b2.departure_date)
OR (date(b1.departure_date) <= b2.arrival_date AND date(b1.arrival_date) >= b2.arrival_date);
您可以發佈來自每個表的小行樣本,以及您希望查詢輸出看起來像什麼樣本?如果可能,請在http://sqlfiddle.com上設置 –