为什么这个看似合法的MySQL Query会出错?

时间:2016-08-09 19:11:45

标签: mysql sql

首先,我有一个名为目标

的表
+----+-----------+---------------------+---------------------+
| id | member_id | created_at          | updated_at          |
+----+-----------+---------------------+---------------------+
|  8 |         4 | 2016-08-08 15:36:06 | 2016-08-08 15:36:06 |
|  9 |         4 | 2016-08-08 15:38:37 | 2016-08-08 15:38:37 |
| 10 |         4 | 2016-08-08 15:44:13 | 2016-08-08 15:44:13 |
| 11 |         4 | 2016-08-08 16:45:14 | 2016-08-08 16:45:14 |
| 12 |         4 | 2016-08-08 16:54:10 | 2016-08-08 16:54:10 |
| 13 |         4 | 2016-08-08 17:04:13 | 2016-08-08 17:04:13 |
| 14 |         4 | 2016-08-08 17:08:09 | 2016-08-08 17:08:09 |
| 15 |         4 | 2016-08-08 17:11:17 | 2016-08-08 17:11:17 |
| 16 |         4 | 2016-08-08 17:16:53 | 2016-08-08 17:16:53 |
| 17 |         4 | 2016-08-08 17:19:39 | 2016-08-08 17:19:39 |
| 18 |         4 | 2016-08-08 17:30:53 | 2016-08-08 17:30:53 |
| 19 |         4 | 2016-08-08 17:37:27 | 2016-08-08 17:37:27 |
| 20 |         4 | 2016-08-09 09:32:09 | 2016-08-09 09:32:09 |
| 21 |         4 | 2016-08-09 12:08:29 | 2016-08-09 12:08:29 |
| 22 |         4 | 2016-08-09 12:16:51 | 2016-08-09 12:16:51 |
| 23 |         4 | 2016-08-09 12:27:48 | 2016-08-09 12:27:48 |
| 24 |         4 | 2016-08-09 12:41:59 | 2016-08-09 12:41:59 |
+----+-----------+---------------------+---------------------+
# ... other rows omitted for brevity

然后是另一张名为 goal_cycles

的表格
+----+---------+---------------------+---------------------+--------------+
| id | goal_id | start_date          | end_date            | cycle_status |
+----+---------+---------------------+---------------------+--------------+
| 14 |       8 | 2016-08-09 15:35:59 | 2016-08-18 15:35:59 | active       |
| 15 |       9 | 2016-08-09 15:38:33 | 2016-08-13 15:38:33 | active       |
| 16 |      10 | 2016-08-09 15:44:11 | 2016-08-11 15:44:11 | active       |
| 17 |      11 | 2016-08-09 16:45:11 | 2016-08-14 16:45:11 | active       |
| 18 |      12 | 2016-08-09 16:54:08 | 2016-08-13 16:54:08 | active       |
| 19 |      13 | 2016-08-09 17:04:11 | 2016-08-13 17:04:11 | active       |
| 20 |      14 | 2016-08-09 17:08:06 | 2016-08-12 17:08:06 | active       |
| 21 |      15 | 2016-08-09 17:11:13 | 2016-08-10 17:11:13 | active       |
| 22 |      16 | 2016-08-09 17:16:50 | 2016-08-10 17:16:50 | active       |
| 23 |      17 | 2016-08-09 17:19:36 | 2016-08-16 17:19:36 | active       |
| 24 |      18 | 2016-08-09 17:30:50 | 2016-08-12 17:30:50 | active       |
| 25 |      19 | 2016-08-09 17:37:23 | 2016-08-12 17:37:23 | active       |
| 26 |      20 | 2016-08-10 09:32:06 | 2016-08-14 09:32:06 | passive      |
| 27 |      21 | 2016-08-10 12:08:26 | 2016-08-19 12:08:26 | passive      |
| 28 |      22 | 2016-08-10 12:16:48 | 2016-08-14 12:16:48 | passive      |
| 29 |      23 | 2016-08-10 12:27:44 | 2016-08-14 12:27:44 | passive      |
| 30 |      24 | 2016-08-10 12:41:54 | 2016-08-19 12:41:54 | passive      |
+----+---------+---------------------+---------------------+--------------+
# ... other rows omitted for brevity

THE TASK :我需要为所有 目标 更新 goal_cycles ,其中 memberId = 4 以及以下方式 cycle_status = active 的位置;

  1. 更新cycle_status = passive
  2. 设置start_date = start_date + 3days
  3. 设置end_date = end_date + 3days
  4. 所以我编写了以下MySQL查询;

    update goal_cycles gc 
        set gc.cycle_status = 'passive', 
        set gc.start_date = DATE_ADD(gc.start_date, INTERVAL 3 DAY), 
        set gc.end_date = DATE_ADD(gc.end_date, INTERVAL 3 DAY) 
    where gc.goal_id in (select g.id from goals g where g.member_id = 4)
    and gc.cycle_status = 'active'
    

    在我看来,这似乎是一个合法的 mysql查询 ,但我收到以下错误消息:

    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set gc.start_date = DATE_ADD(gc.start_date, INTERVAL 3 DAY),
    set gc.end_date = ' at line 3
    

    我是否需要MySQL专家指出此查询的确切问题以及如何根据任务要求正确修复它。

1 个答案:

答案 0 :(得分:10)

你重复了set。它应该只出现在查询中的ONCE:

update table foo
    set field1 = value1,
        field2 = value2,
        field3 = value3,
        etc...

所以,不,这不是有效的语法。