在间隔时间内更新MySQL中的行

时间:2016-01-12 13:57:21

标签: mysql date sql-update

我需要在字段tbl_XX60Valid的Mysql表ccp中进行更新,我已尝试过此查询:

UPDATE `tbl_XX60` CA
JOIN `tbl_users` A ON A.UserNumber = CA.UserNumber
SET CA.Valid = 1,
 CA.ccp = 'y'
WHERE
    CA.dateReg BETWEEN DATE_SUB('2016-01-11', INTERVAL 7 DAY)
AND '2016-01-11'
AND (
    HOUR(CA.HourReg) >= '09'
    OR HOUR(CA.HourReg) <= '21'
)
AND A.PR_Cod IN ('054');

我在where子句查询中应用了三个条件:

 1. CA.dateReg BETWEEN DATE_SUB('2016-01-11', INTERVAL 7 DAY) AND
    '2016-01-11'
 2. HOUR(CA.HourReg) >= '09' OR HOUR(CA.HourReg) <= '21'
 3. A.PR_Cod IN ('054')

没有成功,因为在表tbl_XX60中我发现这些行的更新不包含所选小时的间隔>= '09' OR <= '21'

+--------------+------------+----------+-----+-------+
| UserNumber   | dateReg    | HourReg  | ccp | VALID |
+--------------+------------+----------+-----+-------+
| MPQR16000509 | 2016-01-11 | 02:47:24 | y   |     1 |
| MPQR16000643 | 2016-01-11 | 21:04:01 | y   |     1 |
| MPQR16000647 | 2016-01-11 | 21:21:57 | y   |     1 |
| MPQR16000665 | 2016-01-11 | 21:31:00 | y   |     1 |
| MPQR16000649 | 2016-01-11 | 21:43:08 | y   |     1 |
| MPQR16000651 | 2016-01-11 | 22:22:13 | y   |     1 |
| MPQR16000653 | 2016-01-11 | 22:25:35 | y   |     1 |
| MPQR16000659 | 2016-01-11 | 22:38:02 | y   |     1 |
| MPQR16000661 | 2016-01-11 | 23:04:16 | y   |     1 |
+--------------+------------+----------+-----+-------+

请帮帮我,谢谢你。

#Edit 1

我使用AND而不是OR,但返回不正确,前四个记录未更新,因为&gt; 21小时。

+--------------+------------+----------+-----+-------+
| UserNumber   | dateReg    | HourReg  | ccp | Valid |
+--------------+------------+----------+-----+-------+
| MPQR16000649 | 2016-01-11 | 21:43:08 | y   |     1 |
| MPQR16000665 | 2016-01-11 | 21:31:00 | y   |     1 |
| MPQR16000647 | 2016-01-11 | 21:21:57 | y   |     1 |
| MPQR16000643 | 2016-01-11 | 21:04:01 | y   |     1 |
| MPQR16000629 | 2016-01-11 | 20:09:32 | y   |     1 |
| MPQR16000623 | 2016-01-11 | 20:07:07 | y   |     1 |
| MPQR16000509 | 2016-01-11 | 02:47:24 | N   |     0 |
| MPQR16000549 | 2016-01-11 | 11:29:43 | y   |     1 |
| MPQR16000551 | 2016-01-11 | 11:32:37 | y   |     1 |
| MPQR16000569 | 2016-01-11 | 12:49:02 | y   |     1 |
| MPQR16000571 | 2016-01-11 | 13:09:47 | y   |     1 |
| MPQR16000573 | 2016-01-11 | 13:26:46 | y   |     1 |
| MPQR16000585 | 2016-01-11 | 15:33:14 | y   |     1 |
| MPQR16000651 | 2016-01-11 | 22:22:13 | N   |     0 |
| MPQR16000653 | 2016-01-11 | 22:25:35 | N   |     0 |
| MPQR16000659 | 2016-01-11 | 22:38:02 | N   |     0 |
| MPQR16000661 | 2016-01-11 | 23:04:16 | N   |     0 |
+--------------+------------+----------+-----+-------+
17 rows in set

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用AND代替OR

UPDATE `tbl_XX60` CA
JOIN `tbl_users` A ON A.UserNumber = CA.UserNumber
SET CA.Valid = 1,
 CA.ccp = 'y'
WHERE
    CA.dateReg BETWEEN DATE_SUB('2016-01-11', INTERVAL 7 DAY)
AND '2016-01-11'
AND (
    HOUR(CA.HourReg) >= '09'
    AND HOUR(CA.HourReg) <= '21'
)
AND A.PR_Cod IN ('054');

如果您需要从09:00:00到21:59:59的间隔,则需要使用

AND (
    HOUR(CA.HourReg) >= '09'
    AND HOUR(CA.HourReg) < '22'
)