将SQL与INSERT INTO一起使用时出现错误1064 ... SELECT ...重复键更新

时间:2018-11-27 12:51:52

标签: mysql sql mariadb

我正在尝试使用“ INSERT INTO ... SELECT ON DUPLICATE KEY UPDATE功能”,但现在遇到麻烦了。

我想将数据插入“ fruitProperty”表。

我的查询如下:

START TRANSACTION;

SET @myVal1 := "";
SET @myVal2 := 0;
SET @myVal3 := 0;
SET @myVal4 := 0;
SET @myVal5 := 0;

SELECT masterIndex INTO @myVal1 FROM fruitMaster WHERE masterName = 'apple';
SELECT masterIndex INTO @myVal2 FROM fruitMaster WHERE masterName = 'banana';
SELECT masterIndex INTO @myVal3 FROM fruitMaster WHERE masterName = 'mango';
SELECT masterIndex INTO @myVal4 FROM fruitMaster WHERE masterName = 'melon';
SELECT masterIndex INTO @myVal5 FROM fruitMaster WHERE masterName = 'grape';

INSERT
INTO    fruitProperty
        (fruitID, masterIndex, cpValue)
SELECT  A1.fruitID, A2.masterIndex, A2.cpValue
FROM    (   
           SELECT   A.fruitID
           FROM     fruit A
                    JOIN fruitProperty B ON A.fruitID = B.fruitID
           WHERE    B.masterIndex = @myVal1 AND B.cpValue = 1
        ) A1
        CROSS JOIN
        (
           SELECT @myVal2 AS masterIndex, 1 AS cpValue
           UNION
           SELECT @myVal3, 1
           UNION
           SELECT @myVal4, 1
           UNION
           SELECT @myVal5, 1
       ) A2
ON DUPLICATE KEY UPDATE cpValue = cpValue + 1;

ROLLBACK;

我遇到了错误代码。

  

错误代码:1064   您的SQL语法有误;检查   对应于您的MariaDB服务器版本的手册   在第21行的“ KEY UPDATE cpValue = 1”附近使用的语法

我的查询出了什么问题?我真的不知道。.

谢谢。

2 个答案:

答案 0 :(得分:1)

使用显式join会遇到问题吗?

INSERT INTO fruitProperty (fruitID, masterIndex, cpValue)
SELECT f.fruitID, A2.masterIndex, A2.cpValue
FROM (SELECT f.fruitID
      FROM fruit f JOIN
           fruitProperty fp
            ON f.fruitID = fp.fruitID
      WHERE f.masterIndex = @myVal1 AND fp.cpValue = 1
     ) f JOIN
     (SELECT @myVal2 AS masterIndex, 1 AS cpValue
      UNION ALL
      SELECT @myVal3, 1
      UNION ALL
      SELECT @myVal4, 1
      UNION ALL
      SELECT @myVal5, 1
     ) A2
     ON 1=1
ON DUPLICATE KEY UPDATE cpValue = VALUES(cpValue) + 1;

我怀疑该问题是解析问题,因为MySQL / MariaDB支持ON的{​​{1}}子句(糟糕!)。但是CROSS JOIN关键字令人困惑。

答案 1 :(得分:1)

也许您可以不用使用CROSS JOIN来简化它。

在MySql中,CROSS JOIN只是INNER JOIN的同义词。

但是您不希望将最后一个ON关键字作为JOIN的一部分来混淆。

样本数据

create table fruitMaster (masterIndex int primary key, masterName varchar(30));

insert into fruitMaster (masterIndex, masterName) values 
(1, 'apple'),(2, 'banana'),(3, 'mango'),(4, 'melon'),(5, 'grape'), (6, 'prune');

create table fruit (fruitID int primary key, fruitName varchar(30));

insert into fruit (fruitID, fruitName) values 
(10,'jonagold'),(20,'straight banana'),(40,'big melons');

create table fruitProperty (
 fruitID int, masterIndex int, cpValue int,
 primary key (fruitID, masterIndex));

insert into fruitProperty (fruitID, masterIndex, cpValue) values
(10, 1, 1),(10, 2, 1),(10, 6, 1),
(20, 2, 1),(30, 3, 1),(40, 4, 1);

插入查询

INSERT INTO fruitProperty (fruitID, masterIndex, cpValue)
SELECT F.fruitID, FM2.masterIndex, 1 AS cpValue
FROM fruit F
JOIN fruitProperty FP ON (FP.fruitID = F.fruitID AND FP.cpValue = 1)
JOIN fruitMaster FM1 ON (FM1.masterIndex = FP.masterIndex AND FM1.masterName = 'apple')
JOIN fruitMaster FM2 ON FM2.masterName IN ('banana', 'mango', 'melon', 'grape')
ON DUPLICATE KEY UPDATE cpValue = 2;

结果:

SELECT * FROM fruitProperty;
fruitID | masterIndex | cpValue
------: | ----------: | ------:
     10 |           1 |       1
     10 |           2 |       2
     10 |           3 |       1
     10 |           4 |       1
     10 |           5 |       1
     10 |           6 |       1
     20 |           2 |       1
     30 |           3 |       1
     40 |           4 |       1

db <>提琴here