DolDurma
DolDurma

Reputation: 17301

MySQL twice update with one command

In the below Update command i want to set all delay field to 0 and an id of 2 must be update to 1

enter image description here

UPDATE `tsms_entry_exit` 
         SET `delay`=( 
                      CASE `delay`  
                           WHEN `id` = 2 THEN 1 
                      ELSE 0 
                      END
                     )
WHERE user_id = 1 

Otherwise:

UPDATE `tsms_entry_exit` 
         SET `delay`=0
WHERE user_id = 1;

UPDATE `tsms_entry_exit` 
         SET `delay`=0
WHERE user_id = 1 AND id = 2

How do i update twice in one update command?

Upvotes: 0

Views: 68

Answers (2)

Sadikhasan
Sadikhasan

Reputation: 18600

If I understand very well, might be you want this

UPDATE `tsms_entry_exit` 
         SET `delay`= CASE WHEN `id` = 2 THEN 1 
           ELSE 0 
         END
WHERE user_id = 1 

Upvotes: 1

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

This is how you can use the update command

update tsms_entry_exit set
delay = 
  case when id = 2 then 1
  else 0
end
where user_id = 1 

DEMO

Upvotes: 0

Related Questions