Possible is, but the syntax is terrible and I wouldn’t recommend it :
UPDATE usuarios SET nome = (
CASE id WHEN 1 THEN 'Matheus Silva'
WHEN 2 THEN 'Lucas Silva'
END)
WHERE
id IN (1,2);
Imagine managing this kind of query
.
The ideal would be to querys
separated by ;
like the @Hoppy replied.
$query = "
UPDATE usuarios SET nome='Matheus Silva' WHERE id=1;
UPDATE usuarios SET nome='Lucas Silva' WHERE id=2;
";
$mysqli->multi_query($query);
OBS
Mysql has a specific option for this type of execution which is the multi_query
, which allows the execution of several querys
in the same string
, if the traditional is used query
only the first will be executed.
Tip @Bacco
Or send two processes to the bank.
$query = "UPDATE usuarios SET nome='Matheus Silva' WHERE id=1;"
$this->execute($query);
$query = "UPDATE usuarios SET nome='Lucas Silva' WHERE id=2;";
$this->execute($query);
Do not join 2 updates this way. Separate the updates in different querys.
– user28595
But there is no way to join two updates in just one query no?
– Viniam
Have a look at http://stackoverflow.com/questions/5178169/sql-multiple-sets-in-one-update
– user28595
I took a look at the link but unfortunately it does not solve my problem :(
– Viniam