3

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')

2 Answers2

6

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);
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
0

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.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • @TalalFarhat you are using UPDATE to update status , if status update is not possible after first how you are doing it ? or you are facing problem when you are using boolean for status ? – Prafulla Kumar Sahu Dec 28 '15 at 09:31