Page 1 of 1

REPLACE only part of a row

Posted: Thu Jun 21, 2007 10:11 pm
by superdezign
The REPLACE action makes it so that instead of querying to see if a row exists before inserting/updating, I can do it all in one query. However, I can't keep any of the data the same.

Code: Select all

REPLACE `table` (`primary`, `newstuff`, `oldstuff`) VALUES ($id, $newStuff, `oldstuff`);
This doesn't keep what's in the `oldstuff` column. Is there any way to do this without breaking it up into multiple queries?

Posted: Thu Jun 21, 2007 10:14 pm
by Benjamin
I don't think there is. You can't really use IF NOT EXISTS update else insert in MySQL. If there is a way I would love to know. Your best course of action is to probably use a stored procedure.

Posted: Thu Jun 21, 2007 10:31 pm
by feyd
What about INSERT..ON DUPLICATE KEY...?

Posted: Thu Jun 21, 2007 10:46 pm
by Benjamin

Code: Select all

INSERT INTO t (a,b,c,d,e) VALUES (1,2,3,4,5)
ON DUPLICATE KEY UPDATE c=3,d=4,e=5;
Might work for you. (From user comments: http://dev.mysql.com/doc/refman/5.0/en/ ... icate.html) I've looked into that before but it wouldn't work in my situation.

Posted: Fri Jun 22, 2007 7:42 am
by superdezign
Oooh. Nice. :D