I'm building a reservation system where I am trying to write a query that can insert checkIn & checkOut on condition that don't have conflict.
STRUCTURE
id order_id room_id first_date last_date
-- -------- ------- ---------- ----------
1 00001 A 2015-01-01 2015-01-02
PHP
$dbh->beginTransaction();
$stmt = $dbh->prepare('INSERT INTO date_tbl
(order_id, room_id, first_night, last_night)
VALUE (:order_id, :room_id, :first_night, :last_night) ');
$count = count($first_date);
$i = 0;
for($i;$i<$count;$i++) {
$stmt->execute([
':order_id' => $this->order_id,
':room_id' => $room_id,
':first_night' => $first_date[$i]->format('Y-m-d'),
':last_night' => $last_date[$i]->format('Y-m-d')
]);
}
$dbh->commit();
here is where I am having problem. I want to write
WHERE first_night > each row's last_night
OR last_night < each row's first_night
I understand there is no WHERE in INSERT, how do I do it?
EDIT:
INSERT INTO date_tbl
(order_id, room_id, first_night, last_night)
VALUE (:order_id, :room_id, :first_night, :last_night)
SELECT first_night, last_night FROM date_tbl
WHERE :first_night > first_night
OR :last_night < last_night
maybe this is how you do it???