Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update multiple column with condition in a single sql query

Tags:

mysql

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?

like image 680
kailash Chandra Avatar asked Oct 18 '22 04:10

kailash Chandra


1 Answers

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
)
like image 121
NEER Avatar answered Oct 21 '22 06:10

NEER