Mahmoud Zalt
Mahmoud Zalt

Reputation: 31160

is it possible to update 2 fields in the same column in one query (mysql)

I have table called 'system' has 2 columns 'variable' and 'value' with the following data:

I want to update these 2 fields in 1 query.

the 2 queries are:

UPDATE System SET Value = 'myuser' WHERE Variable = 'Username'
UPDATE System SET Value = 'mypass' WHERE Variable = 'Password'

Upvotes: 1

Views: 209

Answers (5)

Doggy
Doggy

Reputation: 23

UPDATE System SET `Value` = IF(`Variable`='Username', 'myuser', 'mypassw') 
WHERE `Value` IN ('Username', 'Password');

Upvotes: 0

wala rawashdeh
wala rawashdeh

Reputation: 433

UPDATE System SET
 Value = (case Variable when 'Username' then 'myuser' when 'Password' then 'mypass' end)
WHERE Variable  = 'Username' or Variable = 'Password'

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270021

Yes, using he case statement:

UPDATE System
    SET Value = (case when Variable = 'UserName' then 'myuser'
                      when Variable = 'Password' then 'mypass'
                      else Value
                 end)
    WHERE Variable in ('Username', 'Password');

Upvotes: 2

juergen d
juergen d

Reputation: 204794

UPDATE System 
SET Value = case when Variable = 'Username' then 'myuser' 
                 when Variable = 'Password' then 'mypass' 
            end
WHERE Variable in ('Username', 'Password')

Upvotes: 0

Amit Singh
Amit Singh

Reputation: 8109

UPDATE System
SET Value = (CASE
    WHEN Variable = 'Username' THEN 'myuser'
    WHEN Variable = 'Password' THEN 'mypass'
  END)
 WHERE Variable = 'Username' or Variable = 'Password'

Upvotes: 4

Related Questions