无法创建要更新的WHERE子句

时间:2017-11-20 15:09:24

标签: mysql

我正在尝试创建一个WHERE子句,该子句仅允许在NOW()之前1小时到NOW()之后4小时以及上午7点到晚上8点之间。我想要更新的属性是date_time TIME类型。

UPDATE lot
SET userid = 'UID00001', 
    lot_status = 'reserved', 
    date_time = '2017-11-20 19:30:00'
WHERE carparkid = 'CID00001' 
  AND lot_num = 1 
  AND lot_status = 'empty' 
  AND (date_time BETWEEN DATE_SUB(NOW(), INTERVAL 1 HOUR) 
                     AND DATE_ADD(NOW(), INTERVAL 4 HOUR) 
       OR (DATE_FORMAT(date_time, '%k:%i:%s') BETWEEN '07:00:00' 
                                                  AND '20:00:00')
      );

我尝试了很多其他方法,但仍然无法插入我的元组。我根据当前时间插入。

我目前的时间是10:48

MariaDB [carparkful]> UPDATE lot
    -> SET userid = 'UID00001', lot_status = 'reserved', date_time = '2017-11-21 10:30:00'
    -> WHERE carparkid = 'CID00001' AND lot_num = 1 AND lot_status = 'empty' AND (date_time BETWEEN  DATE_SUB(NOW(), INTERVAL 1 HOUR) AND DATE_ADD(NOW(), INTERVAL 4 HOUR)
    -> OR (DATE_FORMAT(date_time, '%k:%i:%s') BETWEEN '07:00:00' AND '20:00:00'));
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

MariaDB [carparkful]> UPDATE lot
    -> SET userid = 'UID00001', lot_status = 'reserved', date_time = '2017-11-21 10:30:00'
    -> WHERE carparkid = 'CID00001' AND lot_num = 1 AND lot_status = 'empty' AND (date_time BETWEEN  DATE_SUB(NOW(), INTERVAL 1 HOUR) AND DATE_ADD(NOW(), INTERVAL 4 HOUR)
    -> AND (DATE_FORMAT(date_time, '%k:%i:%s') BETWEEN '07:00:00' AND '20:00:00'));
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

CREATE TABLE lot
(carparkid VARCHAR(10) NOT NULL, userid VARCHAR(10), lot_num INT(3) NOT NULL, lot_status ENUM('empty', 'reserved', 'occupied') DEFAULT 'empty', date_time DATETIME);

MariaDB [carparkful]> select*from lot;
+-----------+--------+---------+------------+-----------+
| carparkid | userid | lot_num | lot_status | date_time |
+-----------+--------+---------+------------+-----------+
| CID00001  | NULL   |       1 | empty      | NULL      |
| CID00001  | NULL   |       2 | empty      | NULL      |
| CID00001  | NULL   |       3 | empty      | NULL      |
| CID00001  | NULL   |       4 | empty      | NULL      |
| CID00001  | NULL   |       5 | empty      | NULL      |
| CID00002  | NULL   |       1 | empty      | NULL      |
| CID00002  | NULL   |       2 | empty      | NULL      |
| CID00002  | NULL   |       3 | empty      | NULL      |
| CID00002  | NULL   |       4 | empty      | NULL      |
| CID00002  | NULL   |       5 | empty      | NULL      |
| CID00003  | NULL   |       1 | empty      | NULL      |
| CID00003  | NULL   |       2 | empty      | NULL      |
| CID00003  | NULL   |       3 | empty      | NULL      |
| CID00003  | NULL   |       4 | empty      | NULL      |
| CID00003  | NULL   |       5 | empty      | NULL      |
.........

0 个答案:

没有答案