第2行中的语法错误(1064)

时间:2017-09-22 07:31:02

标签: mysql database syntax-error

我正在尝试将查询的表保存到另一个表中。我的新表已经使用以下结构创建:

TABLE: edit_mdr
+---------------+----------------+---------------------------------+
|Column         | Type           | Comment                         |
+---------------+----------------+---------------------------------+
| ID            | int(10)        | unsigned zerofill Auto Increment|
+---------------+----------------+---------------------------------+
| date_submitted| date           |                                 |
+---------------+----------------+---------------------------------+
| requestor     | varchar(40)    |                                 |
+---------------+----------------+---------------------------------+
| campus        | varchar(10)    |                                 | 
+---------------+----------------+---------------------------------+
| defect_loc    | varchar(40)    |                                 |
+---------------+----------------+---------------------------------+
| defect_des    | varchar(1500)  |                                 |
+---------------+----------------+---------------------------------+
| priority      | varchar(10)    |                                 |
+---------------+----------------+---------------------------------+
| action_taken  | varchar(1500)  |                                 |
+---------------+----------------+---------------------------------+
| assigned_to   | varchar(40)    |                                 |
+---------------+----------------+---------------------------------+
| category      | varchar(30)    |                                 |
+---------------+----------------+---------------------------------+
| photo         | varchar(200)   |                                 |
+---------------+----------------+---------------------------------+
| status        | varchar(10)    |                                 |
+---------------+----------------+---------------------------------+

查询如下:

MySQL Query:
INSERT INTO edit_mdr (date_submitted, requestor, campus, defect_loc, defect_des, priority, action_taken, assigned_to, category, photo, status) 
    VALUES(SELECT submit_time,
    MAX(IF(field_name = 'your-name',     field_value, NULL)) AS requestor,
    MAX(IF(field_name = 'menu-campus',   field_value, NULL)) AS campus,
    MAX(IF(field_name = 'location',      field_value, NULL)) AS defect_loc,
    MAX(IF(field_name = 'your-message',  field_value, NULL)) AS defect_des,
    MAX(IF(field_name = 'menu-priority', field_value, NULL)) AS priority,
    MAX(IF(field_name = 'work_done',     field_value, NULL)) AS action_taken,
    MAX(IF(field_name = 'assigned-to',   field_value, NULL)) AS assigned_to,
    MAX(IF(field_name = 'category',      field_value, NULL)) AS category,
    MAX(IF(field_name = 'file-photo',    field_value, NULL)) AS photo,
    MAX(IF(field_name = 'status',        field_value, NULL)) AS status
FROM
    wpc5_cf7dbplugin_submits
GROUP BY
    submit_time);

返回的错误是

  

查询错误(1064):'SELECT submit_time附近出现语法错误,   第2行的MAX(IF(field_name ='your-name',field_value,NULL)'

我不知道第二行发生了什么错误......请帮忙!

2 个答案:

答案 0 :(得分:0)

尝试避免'VALUES'

INSERT INTO edit_mdr (date_submitted, requestor, campus, defect_loc, defect_des, priority, action_taken, assigned_to, category, photo, status) 
    (SELECT submit_time,
    MAX(IF(field_name = 'your-name',     field_value, NULL)) AS requestor,
    MAX(IF(field_name = 'menu-campus',   field_value, NULL)) AS campus,
    MAX(IF(field_name = 'location',      field_value, NULL)) AS defect_loc,
    MAX(IF(field_name = 'your-message',  field_value, NULL)) AS defect_des,
    MAX(IF(field_name = 'menu-priority', field_value, NULL)) AS priority,
    MAX(IF(field_name = 'work_done',     field_value, NULL)) AS action_taken,
    MAX(IF(field_name = 'assigned-to',   field_value, NULL)) AS assigned_to,
    MAX(IF(field_name = 'category',      field_value, NULL)) AS category,
    MAX(IF(field_name = 'file-photo',    field_value, NULL)) AS photo,
    MAX(IF(field_name = 'status',        field_value, NULL)) AS status
FROM
    wpc5_cf7dbplugin_submits
GROUP BY
    submit_time);

答案 1 :(得分:0)

INSERT INTO edit_mdr (date_submitted, requestor, campus, defect_loc, defect_des, priority, action_taken, assigned_to, category, photo, status) 

      SELECT submit_time,
    MAX(IF(field_name = 'your-name',     field_value, NULL)) AS requestor,
    MAX(IF(field_name = 'menu-campus',   field_value, NULL)) AS campus,
    MAX(IF(field_name = 'location',      field_value, NULL)) AS defect_loc,
    MAX(IF(field_name = 'your-message',  field_value, NULL)) AS defect_des,
    MAX(IF(field_name = 'menu-priority', field_value, NULL)) AS priority,
    MAX(IF(field_name = 'work_done',     field_value, NULL)) AS action_taken,
    MAX(IF(field_name = 'assigned-to',   field_value, NULL)) AS assigned_to,
    MAX(IF(field_name = 'category',      field_value, NULL)) AS category,
    MAX(IF(field_name = 'file-photo',    field_value, NULL)) AS photo,
    MAX(IF(field_name = 'status',        field_value, NULL)) AS status
FROM
    wpc5_cf7dbplugin_submits
GROUP BY
    submit_time
;