Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple row update

I am having a problem updating several rows of a mysql table using a single mysql statement.

I have the rowids i want to update in a string eg.

$ids="id1, id2, id3, id4,...."

and i have my values in another string eg.

$values="str1, str2, str3, str4,....";

(i have more than 30,000 rows to update)

The idea is the row with id1 should be updated with str1 and so on.

How can i fix this?

Thanks

like image 351
Ogugua Belonwu Avatar asked Nov 05 '22 20:11

Ogugua Belonwu


1 Answers

It's going to be a very ugly query but...

update table
set    str = case id
             when id1 then str1
             when id2 then str2
             ...
             end
where  id in (id1, id2, ...)
like image 177
Denis de Bernardy Avatar answered Nov 09 '22 12:11

Denis de Bernardy