Updating multiple lines and fields of Mysql

0

I set up a MySQL Query with the purpose of updating a multi-line field of my Mysql table. It was as follows:

update produtos set (nome) = case codigo
when 5 then 'teste 1'
when 6 then 'teste 2'
when 7 then 'teste 3'
where codigo in (5,6,7)

It worked perfectly.

I would, however, like to update other fields in parallel, such as quantity. How do I match the following query to make this possible?

    
asked by anonymous 20.03.2018 / 15:35

1 answer

3

You just put a comma to each field:

UPDATE produtos SET
    nome = CASE codigo
        WHEN 5 THEN 'teste 1'
        WHEN 6 THEN 'teste 2'
        WHEN 7 THEN 'teste 3'
    END, campo2 = "valor2", campo3 = "valor3"
WHERE codigo IN (5,6,7);

This END before the comma, means the end of CASE , avoid forgetting it to not receive the following error:

  

Error Code: 1064 You have an error in your SQL syntax; check the   manual that corresponds to your MySQL server version for the right   syntax to use near

    
20.03.2018 / 15:39