0 votes
1 view
in SQL by (20.3k points)

I know that you can insert multiple rows at once, is there a way to update multiple rows at once (as in, in one query) in MySQL?

Edit: For example I have the following

Name   id  Col1  Col2

Row1   1    6     1

Row2   2    2     3

Row3   3    9     5

Row4   4    16    8

I want to combine all the following Updates into one query

UPDATE table SET Col1 = 1 WHERE id = 1;

UPDATE table SET Col1 = 2 WHERE id = 2;

UPDATE table SET Col2 = 3 WHERE id = 3;

UPDATE table SET Col1 = 10 WHERE id = 4;

UPDATE table SET Col2 = 12 WHERE id = 4;

1 Answer

0 votes
by (40.3k points)

Using the example mentioned above:

You can try using INSERT, ON DUPLICATE KEY UPDATE. 

INSERT INTO table (id, Col1, Col2) VALUES (1,1,1), (2,2,3), (3,9,3), (4,10,12)

ON DUPLICATE KEY UPDATE Col1=VALUES(Col1), Col2=VALUES(Col2);

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 18, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...