2

I want to update and insert to (in 1 query) my table. I've 1 new row and 2 old records. I use this query (topic):

INSERT INTO table (id, title, url) VALUES (1, "test", "http://"), (2, [it is an old record. I don't want to change its title], "https://"), (3, "helloworld", "mms://") ON DUPLICATE KEY UPDATE title = VALUES(title), url = VALUES(url)

I don't want to change the second record (id = 2) title column, but if I put NULL, DEFAULT or title after comma, title will change to NULL (also if i put nothing after comma, an error will occur)

What can I do? should I use 2 or more queries?

Thank you.

Community
  • 1
  • 1
mrdaliri
  • 7,148
  • 22
  • 73
  • 107

2 Answers2

0

Not 100% clear on your goal but could you not nest a SELECT that would pull back the old title for in place of the text in square brackets?

0

You can use something like:

INSERT INTO table (id, title, url) VALUES (1, "test", "http://"), (2, "", "https://"), (3, "helloworld", "mms://") ON DUPLICATE KEY UPDATE title = IF(VALUES(title)="",title,VALUES(title)), url = VALUES(url)
Dominic
  • 786
  • 6
  • 8