MySql:如果值存在则更新,否则插入

16

我有一些代码长这样。这个表中还有一个自增字段必须保留(它被用在其他表中)。我想简化并优化这段代码。

$query ="SELECT * FROM models WHERE col1 = 'foo'";
$testResult = mysql_query($query) or die('Error, query failed');    

if(mysql_fetch_array($testResult) == NULL){
    //insert...
    $query ="INSERT INTO models (col1, col2, col3)
    VALUES ('foo', 'bar', 'alph')";
    $result = mysql_query($query) or die('Error, query failed');
}else{
    //update...
    $query = "UPDATE models
        SET col1='foo', col2='bar', col3='alph'
        WHERE col1='foo' AND col2='bar'";
        $result = mysql_query($query) or die('Error, query failed');        
}

编辑:主键ID是自动递增的字段。我永远不想改变它。但是,当另一个或多个字段重复时,这就是我想要更新该记录的情况。


“autoincrement field” 是什么意思? - hjpotter92
这个问题可能会有所帮助...如何向具有自增主键的 MySQL 插入数据? - blackandorangecat
4个回答

28

REPLACE INTO 怎么样:

REPLACE INTO models
( col1, col2, col3 )
VALUES
( 'foo', 'bar', 'alpha' )

假设col1是您的主键,如果已存在值为“foo”的行,则会更新另外两列。否则,将插入一行新记录。


replace 的问题在于它假设该条目已经存在。但是使用者希望进行插入或更新——两者都要考虑到。 - kasavbere
1
它的命名不太合适。它不会假设该条目已存在。根据MySQL文档:“REPLACE的工作方式与INSERT完全相同,只是如果表中的旧行具有与PRIMARY KEY或UNIQUE索引的新行相同的值,则在插入新行之前删除旧行。” - bobwienholt
我不知道这个。点赞给你——今天我学到了新东西! - kasavbere
1
我认为“REPLACE”在某个时候使用了“DELETE”。我不会给数据库用户DELETE权限,因此在这种情况下REPLACE无法工作。 - Engin Yapici

8
您只需要在重复时更新col3。这正是您所要求的。
INSERT INTO models (col1, col2, col3)
VALUES ('foo', 'bar', 'alpha')
ON DUPLICATE KEY UPDATE col3='alpha';

1
有没有一种方法可以不使用“on duplicate key”来实现这个,比如说你能不能做一些类似于“on duplicate column”的操作呢?我问这个是因为我显然正在自动递增键。 - Kristofer Doman

7

您可以尝试使用"INSERT ... ON DUPLICATE KEY UPDATE"语法。

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have identical effect:

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

UPDATE table SET c=c+1 WHERE a=1;
The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.

With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row and 2 if an existing row is updated.

If column b is also unique, the INSERT is equivalent to this UPDATE statement instead:

UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;
If a=1 OR b=2 matches several rows, only one row is updated. In general, you should try to avoid using an ON DUPLICATE KEY clause on tables with multiple unique indexes.

You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... UPDATE statement. In other words, VALUES(col_name) in the UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. The VALUES() function is meaningful only in INSERT ... UPDATE statements and returns NULL otherwise. Example:

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
  ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
That statement is identical to the following two statements:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=3;
INSERT INTO table (a,b,c) VALUES (4,5,6)
  ON DUPLICATE KEY UPDATE c=9;
If a table contains an AUTO_INCREMENT column and INSERT ... UPDATE inserts a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. If the statement updates a row instead, LAST_INSERT_ID() is not meaningful. However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
The DELAYED option is ignored when you use ON DUPLICATE KEY UPDATE.

3

自MySQL 4.1版本以来可用 https://dev.mysql.com/doc/refman/4.1/en/insert-on-duplicate.html - baptx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接