1

I want to update multiple column with multiple condition. for.eg.

update student set name='john' where id=10 
update student set name='doe' where id=5 

How to update this in a single statement?

Dharman
  • 30,962
  • 25
  • 85
  • 135
kailash Chandra
  • 279
  • 5
  • 13

2 Answers2

4

Use CASE WHEN

update student 
set name= CASE WHEN id = 5 THEN 'john'
               WHEN id = 10 THEN 'doe'
               ELSE name 
           END
where id in (
    5, 10
)
neer
  • 4,031
  • 6
  • 20
  • 34
1
update tablename 
set coloumn_name1= CASE WHEN coloumn_name = 5 THEN 'john'
               WHEN coloumn_name = 10 THEN 'doe'
               ELSE name 
           END
where coloumn_name in (
    5, 10
)
neer
  • 4,031
  • 6
  • 20
  • 34
Arti
  • 35
  • 1
  • 6