16

I have some code that looks like this. There is also an autoincrement field in the table that I must retain (it is used in other tables). I would like to simplify and optimize this code.

$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');        
}

Edit: The primary key id is the field that is auto incremented. I never want to alter this. However , when another field(s) is/are duplicated, this is when I want to update that record.

John R
  • 2,920
  • 13
  • 48
  • 62
  • 4
    Check out: [INSERT ... ON DUPLICATE KEY UPDATE Syntax](http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) – Josh Apr 10 '12 at 20:08
  • What did you mean by ` an autoincrement field` – hjpotter92 Apr 10 '12 at 20:09
  • This question may be helpful... [How to insert data to MySQL having auto incremented primary key?](https://stackoverflow.com/questions/8753371/how-to-insert-data-to-mysql-having-auto-incremented-primary-key) – blackandorangecat Dec 06 '17 at 19:44

4 Answers4

28

How about REPLACE INTO:

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

Assuming col1 is your primary key, if a row with the value 'foo' already exists, it will update the other two columns. Otherwise it will insert a new row.

bobwienholt
  • 17,420
  • 3
  • 40
  • 48
  • the problem with `replace` is it assumes the entry exists. But the use wants to insert or update -- both. – kasavbere Apr 10 '12 at 20:18
  • 1
    It is poorly named. It doesn't assume the entry exists. From the MySQL docs: "REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted." – bobwienholt Apr 10 '12 at 20:21
  • I did not know that. up vote to you -- I learn something new today! – kasavbere Apr 10 '12 at 20:35
  • 1
    I think 'REPLACE' uses 'DELETE' at some point. I don't give database users DELETE access and REPLACE doesn't work in that case. – Engin Yapici Nov 01 '15 at 00:46
8

You only need to update col3 on duplicate. This is exactly what you are asking for.

INSERT INTO models (col1, col2, col3)
VALUES ('foo', 'bar', 'alpha')
ON DUPLICATE KEY UPDATE col3='alpha';
kasavbere
  • 5,873
  • 14
  • 49
  • 72
  • 1
    Is there a way to do this without using 'on duplicate key', like instead could you do something like 'on duplicate column'? I'm asking because I'm obviously auto incrementing the key. – Kristofer Doman Oct 17 '18 at 18:29
7

you can try to use" INSERT ... ON DUPLICATE KEY UPDATE" syntax

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.
Nesim Razon
  • 9,684
  • 3
  • 36
  • 48
3

For MySQL 5.0 + use : INSERT ON DUPLICATE KEY UPDATE

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

Jake Toolson
  • 480
  • 3
  • 8
  • available since MySQL 4.1 https://dev.mysql.com/doc/refman/4.1/en/insert-on-duplicate.html – baptx Jun 25 '12 at 16:52