MYSQL从SELECT QUERY进入

时间:2015-09-09 13:42:43

标签: mysql sql

我正在尝试从SELECT Query中插入所有值,但我收到语法错误,无法确定问题所在。我的查询如下。错误发生在SELECT scrs.requestid,..... line3

INSERT IGNORE INTO temp_scrconflict (
    requestid,
    conflict,
    scr_remedy,
    `type`,
    workstream,
    `system`,
    status,
    `release`,
    assigned_to,
    `role`,
    xx_role, roleid
) VALUES(
    SELECT
        scrs.requestid,
       'no' AS conflict,
       scr_remedy,
       `type`,
       workstream,
       `system`,
       status,
       scrs.release,
       assigned_to,
       `role`,
       REPLACE(LEFT(ROLE, 7),MID(ROLE,3,2),'XX') AS xx_role,
       LEFT(ROLE, 7) AS roleid
   FROM scrs
   JOIN tblscr_role ON scrs.requestid = tblscr_role.requestid
   WHERE ( scrs.release = 'GROUP6'
      OR scrs.release = 'ER_JUNE15'
      OR scrs.release = 'ER_APR15' ))

2 个答案:

答案 0 :(得分:0)

执行VALUES时不应使用INSERT SELECT

INSERT IGNORE 
into   temp_scrconflict 
       ( 
              requestid, 
              conflict, 
              scr_remedy, 
              type, 
              workstream, 
              system, 
              status, 
              `release`, 
              assigned_to, 
              `role`, 
              xx_role, 
              roleid 
       ) 
SELECT scrs.requestid, 
       'no' AS conflict, 
       scr_remedy, 
       type, 
       workstream, 
       system, 
       status, 
       scrs.RELEASE, 
       assigned_to, 
       `role`, 
       REPLACE(LEFT(role, 7),mid(role,3,2),'XX') AS xx_role, 
       LEFT(role, 7) AS roleid 
FROM   scrs 
JOIN   tblscr_role 
ON     scrs.requestid = tblscr_role.requestid 
WHERE  ( 
              scrs.RELEASE = 'GROUP6' 
       OR     scrs.RELEASE = 'ER_JUNE15' 
       OR     scrs.RELEASE = 'ER_APR15'
)

答案 1 :(得分:0)

根据 https://dev.mysql.com/doc/refman/5.0/en/insert-select.html

这里你不需要VALUES

此外,您应该使用带有反向标记的保留字 http://dev.mysql.com/doc/refman/5.6/en/keywords.html

在您的情况下,typestatusrelease

INSERT IGNORE INTO temp_scrconflict 
(requestid, conflict, scr_remedy,  `type`,  workstream,  system,  `status`,  `release`,  assigned_to,  role, xx_role, roleid)
(
    SELECT  
        scrs.requestid, 'no', scr_remedy,  `type`,  workstream,  system,  `status`,  scrs.`release`,  assigned_to,  role, REPLACE(LEFT(role, 7), MID(role,3,2),'XX'), LEFT(role, 7)
    FROM scrs 
    JOIN tblscr_role ON (scrs.requestid = tblscr_role.requestid) 
    WHERE (
        scrs.`release` = 'GROUP6' 
        OR scrs.`release` = 'ER_JUNE15' 
        OR scrs.`release` = 'ER_APR15'
    )
)