条件上的MySQL INSERT与现有行匹配

时间:2015-09-30 19:03:09

标签: php mysql

我正在建立一个预订系统,我正在尝试编写一个可以插入checkIn&在没有冲突的情况下checkOut。

结构

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();

这是我遇到问题的地方。我想写

WHERE first_night > each row's last_night 
OR last_night < each row's first_night  

我知道INSERT中没有WHERE,我该怎么办?

编辑:

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

也许这是你怎么做的???

0 个答案:

没有答案