SQL - UPDATE double with double where

5

Next, is it possible to do two update-where together in the same query? I'll explain, what I wanted to do is this:

UPDATE usuarios
   SET nome = "Matheus Silva"
 WHERE id = 1
   AND UPDATE usuarios SET nome = "Lucas Silva"
 WHERE id = 2;

But unfortunately it does not work, does anyone know the right way to do it?

    
asked by anonymous 18.01.2016 / 05:06

2 answers

6

Possible is, but syntax is terrible and I would not even 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 type of query . Ideally, make querys separated by ; like @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 multi_query , that allows the execution of several querys in the same string , if the traditional query is used only the first will be executed. @Bacco Tip

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);
    
18.01.2016 / 11:37
3
$sql = "UPDATE usuarios SET nome="Matheus Silva" WHERE id=1; UPDATE usuarios SET nome="Lucas Silva" WHERE id=2;";

I do not know which language you are using .. but try this way by separating queries with;

    
18.01.2016 / 10:06