简体   繁体   English

“ INSERT IF NOT EXISTS”,否则更新MySQL中的某些列

[英]“INSERT IF NOT EXISTS”, ELSE UPDATE certain columns in MySQL

I'm using MySQL Workbench (6.3) and I'm trying to create a stored procedure with a specific "INSERT IF NOT EXSISTS" but I think I don't do it well. 我正在使用MySQL Workbench(6.3),并且尝试创建一个带有特定“如果不存在则插入”的存储过程,但是我认为做得不好。 By the way, I did read this topic http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html and tried both examples, it didn't work. 顺便说一句,我确实阅读了该主题http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html并尝试了两个示例,但都没有用。 Here is the statement : 这是声明:

CREATE DEFINER=`test`@`localhost` PROCEDURE `onPlayerJoin`(
    IN uuid CHAR(36),
    IN nickname CHAR(16),
    IN firstConnection TIMESTAMP,
    IN lastConnection TIMESTAMP
)

BEGIN
    INSERT IGNORE INTO `test`.`player` (`playerUuid`, `playerNickname`, `playerFirstConnection`, `playerLastConnection`)
        VALUES (uuid, nickname, firstConnection, lastConnection);
        UPDATE `test`.`player` SET
        `playerNickname` = nickname,
        `playerLastConnection` = lastConnection
        WHERE `playerUuid` = uuid;
END

IF the PK isn't found, INSERT it. 如果找不到PK,请插入它。 ELSE, UPDATE certain columns. 否则,更新某些列。 (that I can specified) However, it seems that it updates every column, which I would like to restrict to certain columns. (我可以指定)但是,似乎它会更新每个列,我想将其限制为某些列。 Here is my procedure : http://pastebin.com/NfcdU9Rb ! 这是我的过程: http : //pastebin.com/NfcdU9Rb

Optional question : is it injection-safe ? 可选问题:注射安全吗?

Use INSERT ... ON DUPLICATE KEY UPDATE Syntax 使用INSERT ... ON DUPLICATE KEY UPDATE语法

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

Kindly refer the link for details: http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html 请参考该链接以获取详细信息: http : //dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM