I have a question, I want to update values in a table with conditions.
I have a list of users that I need to update their accounting_id
, the key is user_id
.
I want to do it in a single query.
The problem is that the query puts 0 in the accounting id
. The DB is MySQL.
UPDATE users
SET users.accounting_id = CASE
WHEN users.user_id = '102215' THEN users.accounting_id = '102'
WHEN users.user_id = '102144' THEN users.accounting_id = '193'
END
WHERE users.user_id IN ('102215','102144');
Let's examine the expressions in the
then
part of your statement. e.g.:users.accounting_id = '102'
. This is not an assignment, but a boolean expression, which evaluates to false, sinceaccounting_id
is not 102 (it's 102215 in this case). The=
before thecase
is used for the assignment. In thethen
branches you just need to return the value to be assigned: