Home » Mysql » MySQL update CASE WHEN/THEN/ELSE [duplicate]

MySQL update CASE WHEN/THEN/ELSE [duplicate]

Posted by: admin November 1, 2017 Leave a comment

Questions:

This question already has an answer here:

Answers:

Try this

UPDATE `table` SET `uid` = CASE
    WHEN id = 1 THEN 2952
    WHEN id = 2 THEN 4925
    WHEN id = 3 THEN 1592
    ELSE `uid`
    END
WHERE id  in (1,2,3)

Questions:
Answers:

Simpler would be:

UPDATE `table` SET uid = ELT(id, 2952, 4925, 1592) WHERE id IN (1,2,3)

This will only work if uid is indeed 1, 2, 3.

Source: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_elt

Questions:
Answers:

Simpler would be:

UPDATE `table` SET uid = ELT(id, 2952, 4925, 1592) WHERE id IN (1,2,3)

Questions:
Answers:

That’s because you missed ELSE.

“Returns the result for the first condition that is true. If there was no matching result value, the result after ELSE is returned, or NULL if there is no ELSE part.”
(http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#operator_case)