Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bulk update mysql data with one query?

Tags:

php

mysql

$query = mysql_query("UPDATE a SET fruit = '**apple**' WHERE id = '**1**' "); $query2 = mysql_query("UPDATE a SET fruit = '**orange**' WHERE id = '**2**' "); $query3 = mysql_query("UPDATE a SET fruit = '**peach**' WHERE id = '**3**' "); 

is there any way to simplify it to one query?

like image 338
Wei Keat Avatar asked Jul 26 '12 07:07

Wei Keat


1 Answers

I found a following solution:

INSERT into `table` (id,fruit)     VALUES (1,'apple'), (2,'orange'), (3,'peach')     ON DUPLICATE KEY UPDATE fruit = VALUES(fruit); 

Id must be unique or primary key. But don't know about performance.

like image 90
Yaroslav Avatar answered Oct 05 '22 16:10

Yaroslav