2012-04-10 49 views
16

我有一些看起來像這樣的代碼。表中還有一個自動增量字段,我必須保留它(在其他表中使用它)。我想簡化和優化這段代碼。MySql:如果存在值UPDATE else INSERT

$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是自動遞增的字段。我從來不想改變這一點。但是,當另一個字段被複制時,這是我想更新該記錄的時間。

+4

退房:[INSERT ...對重複密鑰更新語法](http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) – Josh 2012-04-10 20:08:59

+0

做什麼你的意思是'自動增量字段' – hjpotter92 2012-04-10 20:09:33

+0

這個問題可能會有幫助... [如何將數據插入到具有自動遞增主鍵的MySQL?](https://stackoverflow.com/questions/8753371/how-to-insert-數據到MySQL擁有自動遞增主鍵) – blackandorangecat 2017-12-06 19:44:25

回答

29

怎麼樣REPLACE INTO:

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

假設col1是您的主鍵,如果具有值'foo'的行已經存在,它將更新其他兩列。否則它會插入一個新行。

+0

問題與'replace'是否假定條目存在。但使用者想要插入或更新 - 都是。 – kasavbere 2012-04-10 20:18:20

+1

它名字很差。它不假定條目存在。從MySQL文檔:「REPLACE的工作方式與INSERT完全相同,只是如果表中的舊行與PRIMARY KEY或UNIQUE索引的新行具有相同的值,則在插入新行之前刪除舊行。 「 – bobwienholt 2012-04-10 20:21:00

+0

我不知道。投票給你 - 今天我學到了一些新東西! – kasavbere 2012-04-10 20:35:37

6

,你可以嘗試使用 「插入...對重複密鑰更新」 語法

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. 
6

您只需要更新重複的col3。這正是你所要求的。

INSERT INTO models (col1, col2, col3) 
VALUES ('foo', 'bar', 'alpha') 
ON DUPLICATE KEY UPDATE col3='alpha';