UPDATE如果存在则在SQL中INSERT

时间:2015-06-01 09:11:22

标签: mysql sql

我试图将SQL查询实现为" UPDATE如果存在,则INSERT"

我的表格(Allowance)如下:

EmployeeID int(8) PK
Year year(4) PK
Month int(2) PK
OverTime decimal(10,2)
Medical decimal(10,2)
Lunch decimal(10,2)
Bonus decimal(10,2)
Allowance decimal(10,2)

下面是我试过的SQL查询:

IF EXISTS (SELECT * FROM Allowance WHERE EmployeeID =10000001 and Year = 2014 and Month = 4)
    UPDATE Allowance
    SET OverTime = 10.00, Medical = 10.00, Lunch = 10.45, Bonus =10.10, Allowance = 40.55
    WHERE EmployeeID =10000001 and Year = 2014 and Month = 4
ELSE
    INSERT into Allowance values (10000001,2014,4,10.00,10.00,10.45,10.10,40.55)

我不断收到此错误消息:

  

"#1064 - 您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以便在#EVIS段附近使用正确的语法(SELECT * FROM Allowance WHERE EmployeeID = 10000001和Year = 2014 an' at line 1"

有人可以帮忙吗?

5 个答案:

答案 0 :(得分:5)

以下查询将满足您的要求。

INSERT INTO `ALLOWANCE` (`EmployeeID`, `Year`, `Month`, `OverTime`,`Medical`,
`Lunch`, `Bonus`, `Allowance`) values (10000001, 2014, 4, 10.00, 10.00,
10.45, 10.10, 40.55) ON DUPLICATE KEY UPDATE `EmployeeID` = 10000001

答案 1 :(得分:2)

进一步了解 Gunaseelan 的回答(回答我自己的问题,我需要查找):

INSERT INTO `ALLOWANCE` (`EmployeeID`, `Year`, `Month`, `OverTime`,`Medical`,
`Lunch`, `Bonus`, `Allowance`) values (10000001, 2014, 4, 10.00, 10.00,
10.45, 10.10, 40.55) ON DUPLICATE KEY UPDATE `OverTime`=10.00, `Medical`=10.00, `Lunch`=10.45, `Bonus`=10.10, `Allowance`=40.55;
  1. 不要忘记结尾的分号(就像我一样);

  2. WHERE 不是必需的 - 它知道哪个记录是重复的

  3. 也无需再次提及表名 - 这是上半年提供的

这是一个更详细地解释语法的页面:

https://chartio.com/resources/tutorials/how-to-insert-if-row-does-not-exist-upsert-in-mysql/#using-insert--on-duplicate-key-update

答案 2 :(得分:1)

使用此程序,您可以检查是否存在,然后根据需要更新/插入

 DELIMITER $$;   
 CREATE PROCEDURE example()

BEGIN
DECLARE vexist int;

  SELECT count(*) into vexist FROM Allowance --count because i will
  WHERE EmployeeID =10000001 and Year = 2014 and Month = 4;  --this will check if exist or not

    IF (vexist >= 1) then  --if exist then update
    UPDATE Allowance
        SET OverTime = 10.00, Medical = 10.00, Lunch = 10.45, Bonus =10.10, Allowance = 40.55
        WHERE EmployeeID =10000001 and Year = 2014 and Month = 4;
    ELSE
      INSERT into Allowance values (10000001,2014,4,10.00,10.00,10.45,10.10,40.55);
END IF;
END $$
DELIMITER ;

您必须立即致电该程序:

call example();

注意:这将解决您现在的解决方案,但这不是最好的想法,因为procedures打算在将来使用,所以我会给你一个改进的版本,在未来你将只能通过调用过程并写入插入值来更新/插入。

DELIMITER $$;   
     CREATE PROCEDURE example(
IN
vempid int(8),
vyear year(4), 
vmonth int(2),
vovertime float(10,2),
vmedical float(10,2),
vlunch float(10,2),
vbonus float(10,2),
vallowance float(10,2))

    BEGIN
    DECLARE vexist int;

      SELECT count(*) into vexist FROM Allowance --count because i will
      WHERE EmployeeID =vemp and Year = vyear and Month = vmonth;  --this will check if exist or not

        IF (vexist >= 1) then  --if exist then update
        UPDATE Allowance
            SET OverTime = vovertime, Medical = vmedical, Lunch = vlunch, Bonus = vbonus, Allowance = vallowabce
            WHERE EmployeeID =10000001 and Year = vyear and Month = vmonth;
        ELSE
          INSERT INTO `ALLOWANCE` (`EmployeeID`, `Year`, `Month`, `OverTime`,`Medical`,`Lunch`, `Bonus`, `Allowance`) values (vempid,vyear,vmonth,vovertime,vmedical,vlunch,vbonus,vallowance);
    END IF;
    END $$
    DELIMITER ;

并调用(使用正确的参数顺序):

call example2(10000001,2014,4,10.00,10.00,10.45,10.10,40.5);

答案 3 :(得分:0)

SELECT IF (EXISTS())代替IF EXISTS()

Usage of MySQL's "IF EXISTS"

答案 4 :(得分:0)

无需为此编写自定义SQL。 MySQL已经有INSERT ... ON DUPLICATE KEY UPDATE,它完全一样。

如果指定ON DUPLICATE KEY UPDATE,并且插入的行会导致UNIQUE索引或PRIMARY KEY中的重复值,则会执行旧行的更新。

请阅读URL了解详情。