Update of multiple rows with different values

1

I'm trying to update different rows with different values, in my sql table, the closest I've come is:

UPDATE cursos    
SET posicion = 11
where Id in (1,2)

But it updates me with the same value (11) logically the two rows, I need to give a different value to each row. Can someone help me with this dilemma? Thanks !!!

    
asked by Patricio 30.08.2017 в 01:12
source

1 answer

2

You can use a conditional, such as CASE ... END, which will assign a value to position according to the id:

UPDATE cursos    
SET posicion = CASE Id
    WHEN 1 THEN 11
    WHEN 2 THEN 15
END
WHERE Id IN (1,2)

In this way, the course with id = 1 will have position = 11 and the course with id = 2 will have position = 15.

    
answered by 30.08.2017 / 01:17
source