Alex Rivera | Logout

MYSQL UPDATE SET on the Same Column but with multiple WHERE Clauses

Asked 2012-02-20T21:14:58.830
15

With MYSQL I'm using this query:

UPDATE CustomerDetails_COPY
SET Category_ID = 10
WHERE Category_ID = 2

Thats fine but I'd like to ad 15+ more SET/WHERE to it like:

UPDATE CustomerDetails_COPY
SET Category_ID = 9  WHERE Category_ID = 3
SET Category_ID = 12 WHERE Category_ID = 4
SET Category_ID = 11 WHERE Category_ID = 5
.....

How would I add to this?

EDIT:

As Per Hunters Suggestion:

UPDATE CustomerDetails_COPY
    SET Category_ID = CASE Category_ID
        WHEN 2 THEN 10 
        WHEN 3 THEN 9
        WHEN 4 THEN 12
        WHEN 5 THEN 11
    END
WHERE Category_ID IN (2,3,4,5)

This works Great! Thanks

Edit
Report

1 Answer

2

The typical way to do this would be

UPDATE CustomerDetails_COPY SET Category_ID = 10 WHERE Category_ID = 2
UPDATE CustomerDetails_COPY SET Category_ID = 9  WHERE Category_ID = 3
UPDATE CustomerDetails_COPY SET Category_ID = 12 WHERE Category_ID = 4
UPDATE CustomerDetails_COPY SET Category_ID = 11 WHERE Category_ID = 5

Is there any reason you are avoiding this approach?

Is there any correlation between the new and old values of category_ID?

answered 2012-02-20T21:20:17.640

Your Answer