Question

I have to merge two tables and update on duplicate key only if new value is not null. I tried the code below with no success Can anyone help? Thanx!

INSERT
INTO    table1
SELECT  * FROM    temp  
ON DUPLICATE KEY UPDATE
  table1.tel = coalesce(temp.tel,table1.tel),
  table1.fax = coalesce(temp.fax,table1.fax)
Was it helpful?

Solution

I think your problem may be the way you are referring to the values in your update clause. The following may work better:

INSERT
INTO    table1
SELECT  * FROM    temp  
ON DUPLICATE KEY UPDATE
  table1.tel = coalesce(values(tel),tel),
  table1.fax = coalesce(values(fax),fax)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top