1

I want to update multiple records at once using a similar method to this:

Multiple Updates in MySQL

but is there a MySQL command to ignore anything that isnt a duplicate? Something like

ON DUPLICATE UPDATE ON UNIQUE IGNORE

(ive just made this code up btw)

?

Community
  • 1
  • 1
Jimmery
  • 9,783
  • 25
  • 83
  • 157

1 Answers1

2

Why can't we use a single UPDATE command that can update MULTIPLE rows!

UPDATE mytable 
SET Col2 = CASE
  WHEN Col1 = 1 THEN 'new Value From someplace';
  WHEN Col1 = 2 THEN 'War and Peace';
  ELSE Col2
END

The ELSE Col2 is very important, otherwise you will overwrite the rest of the table with NULL.

You can add more CASE blocks to update more columns.

By doing this you can avoid the headache of INSERTING UNIQUE records.

Hope this helps.

Jirilmon
  • 1,924
  • 1
  • 12
  • 13
  • excellent, thanks! - im unfamiliar with the CASE / WHEN / THEN syntax, but this does exactly what im trying to do! – Jimmery Jan 18 '13 at 17:01