Hello, I have a table in which there are columns ( u , name, etc.). The script receives data (a lot of data) with values ​​( u , name, etc.). It is necessary to update all the rows in column u with a single query. You need something like this:

UPDATE table_name SET colum_name1 = VALUES('value1', 'value2', ..., 'valuenN'), colum_name2 = VALUES('value1', 'value2', ..., 'valuenN') WHERE colum_name3 IN ('value1', 'value2', ...,'valuenN'); 
  • You are in the conditions of where in the update, ask that it handles all the necessary lines, it will update everything - Mike
  • I added that I roughly need. - Kasper

2 answers 2

I'm afraid just like that:

 UPDATE table_name JOIN ( select 'val1x' as v1, 'val1y' as v2, 'val1z' as v3 union select 'val2x', 'val2y', 'val2z' union select 'val3x', 'val3y', 'val3z' ) A ON A.v3=table_name.colum_name3 SET colum_name1 = A.v1, colum_name2 = A.v2 

Or use REPLACE syntax matching INSERT, provided that colum_name3 is the primary or unique key. But at the same time the records will be deleted and inserted, which may reflect badly on the related tables.

  • Thanks for solving the problem. Only I have a question. Why in the line select 'val1x' as v1, 'val1y' as v2, 'val1z' v3 The last value is missing as - Kasper
  • @Kasper Missed :) In general, these AS are only for clarity, MySQL, like many other DBMS, does not require this, you can write column names without them - Mike

Option 1:

 UPDATE `table` SET `name`= CASE WHEN `name`='test' THEN 'testing' WHEN `name`='test2' THEN 'testing2' WHEN `name`='test3' THEN 'testing3' ELSE `test` END ... 

Option 2:

 INSERT INTO `table` (u, name, age) VALUES ('value1', 'value2', 5), ('value1', 'value2', 28), ('value1', 'value2', 28) ON DUPLICATE KEY UPDATE name = VALUES(name), age = VALUES(age) 

U - unique key

I advise you not to use these constructions, but to perform the update through a single command in the set of transposition commands, so the blocking time for a separate record is much less and it works faster and in fact.

  • and what a set of transactions? - Kasper
  • I did not speak about the "transaction set", I spoke about the instruction set in the transaction. You can execute commands in certain portions, you can execute one by one. - Firepro