Is there any single SQL statement equivalent to these?
UPDATE table_name SET (a = 'something', b='B1') WHERE id=1;
UPDATE table_name SET (a = 'something else', b='B2') WHERE id=2;
UPDATE table_name SET (a = 'another', b='B3') WHERE id=3;
Is there any single SQL statement equivalent to these?
UPDATE table_name SET (a = 'something', b='B1') WHERE id=1;
UPDATE table_name SET (a = 'something else', b='B2') WHERE id=2;
UPDATE table_name SET (a = 'another', b='B3') WHERE id=3;
Yes, this:
UPDATE table_name
SET a = CASE WHEN id = 1
THEN 'something'
WHEN id = 2
THEN 'something else'
WHEN id = 3
THEN 'another'
END
WHERE id IN (1,2,3)
;
but I'm not sure if it's what you have in mind?
If you have bigger array of data to be inserted, then you may use ON DUPLICATE KEY UPDATE
construction. It will work more efficient in MySQL.
See my answer here for the similar question, for the example of usage.
You can use select rows with your constant value, then join it with you table.
UPDATE table_name T INNER JOIN (select 1 as id,'something' as a,'B1' as b
union all select 2,'something','B2'
union all select 3,'another','B2') V on T.id = V.id set T.a = V.a, T.b = V.b;