Talal Farhat
Talal Farhat

Reputation: 99

MySQL combine two UPDATE Queries in one Query

I want to update status, but using one query !!

UPDATE myTable SET `status` = 0 WHERE `name` IN ('a', 'b', 'c', 'd')
UPDATE myTable SET `status` = 1 WHERE `name` NOT IN ('a', 'b', 'c', 'd')

Upvotes: 3

Views: 1161

Answers (2)

Sadikhasan
Sadikhasan

Reputation: 18600

You can use CASE statement.

UPDATE myTable
SET status = (CASE
                  WHEN `name` IN ('a','b','c','d') THEN '0'
                  WHEN `name` NOT IN ('a','b','c','d') THEN '1'
              END);

Upvotes: 6

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9693

Make 'status' datatype boolean, default value 0 and for "name NOT IN ('a', 'b', 'c', 'd')"

use

UPDATE myTable SET `status` = 1 WHERE `name` NOT IN ('a', 'b', 'c', 'd');

You do not have to run two queries.

Upvotes: 0

Related Questions