N
N
Normant2014-12-20 00:40:55
MySQL
Normant, 2014-12-20 00:40:55

How to do CASE WHEN THEN for multiple values?

Friends, a question on databases. Query that works:

UPDATE `col` SET
`nul` = CASE
WHEN (`attr1`,`attr2`) = ('param1','otv1') THEN '1'
WHEN (`attr1`,`attr2`) = ('param1','otv2') THEN '2'
WHEN (`attr1`,`attr2`) = ('param1','otv3') THEN '3'
END
WHERE (`attr1`,`attr2`) IN (('param1','otv1'),('param1','otv2'),('param1','otv3'))

But if you need to enter another column, which you also need to change based on the same data, then how to write it down?
Roughly speaking:
nul = 1 if attr1 == param1 && attr2 == otv1

convert entry to:
nul = 1 && bul = x if attr1 == param1 && attr2 == otv1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mnbck, 2014-12-20
@Normant

UPDATE `col` SET
`nul` = CASE
WHEN (`attr1`,`attr2`) = ('param1','otv1') THEN '1'
WHEN (`attr1`,`attr2`) = ('param1','otv2') THEN '2'
WHEN (`attr1`,`attr2`) = ('param1','otv3') THEN '3'
END,
`bul` = CASE
WHEN (`attr1`,`attr2`) = ('param1','otv1') THEN '1'
WHEN (`attr1`,`attr2`) = ('param1','otv2') THEN '2'
WHEN (`attr1`,`attr2`) = ('param1','otv3') THEN '3'
END
WHERE (`attr1`,`attr2`) IN (('param1','otv1'),('param1','otv2'),('param1','otv3'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question