Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REPLACE into keeping a value

Tags:

mysql

I want to make a replace into in a table where cust_id is the primary key, but I do not want to modify the date field. So, a normal insert on this table would look like:

    REPLACE INTO emails(cust_id, email, date) 
VALUES(55, '[email protected]', '2011-08-07 00:00');

Now, without having to modify the date field, it would be something such as:

REPLACE INTO emails(cust_id, email, date) 
VALUES(55, '[email protected]', date.value?);

But how do I exactly keep the date value?

like image 475
luqita Avatar asked Sep 12 '25 01:09

luqita


2 Answers

Short answer, You can't keep the dates that way. from Mysql documentation

Values for all columns are taken from the values specified in the REPLACE statement. Any missing columns are set to their default values, just as happens for INSERT. You cannot refer to values from the current row and use them in the new row

perhaps you want to use http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html instead.

like image 75
Brian Glaz Avatar answered Sep 14 '25 18:09

Brian Glaz


insert ignore will skip insertion if any duplication

if you need to update certain fields ,
you can do

insert into some_table values (...)
on duplicate update email=?;
like image 33
ajreal Avatar answered Sep 14 '25 19:09

ajreal