简体   繁体   English

如果值存在,则MySQL更新;如果不存在,则插入MySQL?

[英]MySQL Update if value exists, Insert if not in PHP?

$sql_career = "REPLACE INTO career
         (id, battletag, lastHeroPlayed, lastUpdated, monsters, elites, hardcoreMonsters, barbarian, crusader, demonhunter, monk, witchdoctor, wizard, paragonLevel, paragonLevelHardcore)
         VALUES
         ('', '$battletag', '$lastHeroPlayed', '$lastUpdated', '$monsters', '$elites', '$hardcoreMonsters', '$barbarian', '$crusader', '$demonhunter', '$monk', '$witchdoctor', '$wizard', '$paragonLevel', '$paragonLevelHardcore')";

ID auto increments. ID自动递增。 battletag is unique. battletag是独一无二的。

Everything else changes over time. 其他所有事情都会随着时间而改变。 So I want to replace or update an entry if the battletag already exists without it making a new id . 所以我想替换或更新一个条目,如果battletag已经存在而不创建新id If it doesnt exist I want it to make a new entry letting the id auto increment for that unique battletag . 如果它不存在,我希望它创建一个新条目,让id自动为该唯一battletag递增。


This works with one problem: 这可以解决一个问题:

 $sql_career = "
    insert INTO career
      (id, battletag, lastHeroPlayed)
    VALUES
      (NULL, '$battletag', $lastHeroPlayed)
    on duplicate key
      update lastHeroPlayed=$lastHeroPlayed;
 ";

If I, for instance, load in two unique rows, the ID auto increments to 1 and then 2 for each. 例如,如果我在两个唯一的行中加载,则ID自动递增为1,然后每个递增2。 Then if I load up a row that has a duplicate of the unique key of one of the existing rows (and it then updates as it should) this actually triggers the auto increment. 然后,如果我加载的行与现有行之一的唯一键重复(然后按需要更新),则实际上会触发自动递增。 So if I then add in a third unique row, its number will be 4 instead of 3. 因此,如果我再添加第三行唯一行,则其编号将是4而不是3。

How can I fix this? 我怎样才能解决这个问题?

You want to use the on duplicate key ... update syntax instead of replace into. 您想使用on duplicate key ... update 语法,而不是replace into。

Define a unique column (primary or unique index) then check it in your statement like this: 定义一个唯一列(主索引或唯一索引),然后在您的语句中检查它,如下所示:

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

The benefit of using this over a replace into is that replace into will always delete the data you have already and replace it (sort of as the command name implies) with the data that you are supplying the second time round. 使用此替换项而不是替换为替换项的好处是,替换为将始终删除您已经拥有的数据,并将其替换(如命令名所示),将其替换为第二次提供的数据。 An update on... statement however will only update the columns you define in the second part of it - if the duplicate is found - so you can keep information in the columns you want to keep it in. 但是, update on...语句只会更新在第二部分中定义的列(如果找到重复的列),因此您可以将信息保留在要保留的列中。

Basically your command will look something like this (Abbreviated for important columns only) 基本上,您的命令将类似于以下内容(仅在重要列中缩写)

$sql_career = "
    insert INTO career
        (id, battletag, heroesKilled)
    VALUES
        ($id, '$battletag', $heroesKilled)
    on duplicate key
        update heroesKilled=heroesKilled+1;

";

Again, remember that in your table, you will need to enforce a unique column on battletag - either a primary key or unique index . 再次提醒您,在表中,您将需要在Battletag上强制使用唯一列- 主键或唯一索引 You can do this once via code or via something like phpMyAdmin if you have that installed. 您可以通过代码或通过phpMyAdmin之类的代码(如果已安装)执行一次。

Edit: Okay, I potentially found a little gem (it's about a third of the way down the page) that might do the trick - never used it myself though, but can you try the following for me? 编辑:好的,我可能发现了一个小小的宝石 (大约是页面的三分之一),可以解决问题-虽然我自己从未使用过,但是可以为我试试以下方法吗?

$sql_career = "
    insert ignore INTO career
        (id, battletag, heroesKilled)
    VALUES
        (null, '$battletag', $heroesKilled)
    on duplicate key
        update heroesKilled=heroesKilled+1;

";

There seems to be collaborating evidence supporting this in this page of the docs as well: 在文档的此页面中似乎也有协作证据支持这一点

If you use INSERT IGNORE and the row is ignored, the AUTO_INCREMENT counter is not incremented and LAST_INSERT_ID() returns 0, which reflects that no row was inserted. 如果使用INSERT IGNORE并且忽略该行,则AUTO_INCREMENT计数器不会增加,并且LAST_INSERT_ID()返回0,这表明没有插入行。

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

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