25

Can anybody tell me how to fix this query?

update ae44
set Price = Case
when ID = 'AT259793380' then '500'
when ID = 'AT271729590' then '600'
when ID = 'AT275981020' then '700'
end case

I just want to mass update listing price based on ID by matching up ID.

A-Tech
  • 806
  • 6
  • 22

7 Answers7

40
UPDATE  ae44
SET     price =
        CASE
        WHEN ID = 'AT259793380' THEN
                '500'
        WHEN ID = 'AT271729590' THEN
                '600'
        WHEN ID = 'AT275981020' THEN
                '700'
        END

Note than this query will update prices for the records with other ids to NULL.

If you only want to update only ids from your list, use this:

UPDATE  ae44
JOIN    (
        SELECT  'AT259793380' AS oldval, '500' AS newval
        UNION ALL
        SELECT  'AT271729590' AS oldval, '600' AS newval
        UNION ALL
        SELECT  'AT275981020' AS oldval, '700' AS newval
        ) q
ON      ae44.id = q.oldval
SET     price = q.newval
Quassnoi
  • 413,100
  • 91
  • 616
  • 614
27
UPDATE ae44 SET
    Price = CASE
    WHEN ID = 'AT259793380' THEN '500'
    WHEN ID = 'AT271729590' THEN '600'
    WHEN ID = 'AT275981020' THEN '700'
    ELSE Price END

Or you can use WHERE:

UPDATE ae44 SET
    Price = CASE
    WHEN ID = 'AT259793380' THEN '500'
    WHEN ID = 'AT271729590' THEN '600'
    WHEN ID = 'AT275981020' THEN '700'
    END
WHERE 
    ID IN ('AT259793380', 'AT271729590', 'AT275981020')

And set the LIMIT is good idea too:

UPDATE ae44 SET
    Price = CASE
    WHEN ID = 'AT259793380' THEN '500'
    WHEN ID = 'AT271729590' THEN '600'
    WHEN ID = 'AT275981020' THEN '700'
    END
WHERE 
    ID IN ('AT259793380', 'AT271729590', 'AT275981020')
LIMIT 3
Sergey
  • 271
  • 3
  • 2
4

Remove the second "case" and it will work:

UPDATE ae44
SET Price = (CASE
WHEN ID = 'AT259793380' THEN '500'
WHEN ID = 'AT271729590' THEN '600'
WHEN ID = 'AT275981020' THEN '700'
END)
Ike Walker
  • 64,401
  • 14
  • 110
  • 109
  • 3
    Be careful with this... it will set the price of any other item in the database to NULL. Add a where statement (set price is (case...) where id in ("AT259793380","AT271729590","AT275981020")) – patrick Jan 31 '13 at 21:54
3

You can try a simple query like:

UPDATE `table`
SET Price = ELT(field(ID,'AT259793380','AT271729590','AT275981020'), '500', '600', '700')
WHERE ID IN ('AT259793380','AT271729590','AT275981020')
josliber
  • 43,891
  • 12
  • 98
  • 133
2

I'm assuming this is a mysql query. You can use the first query that Quassnoi posted and just add

WHEN ID THEN price

as the last "WHEN". This will prevent all of your price fields from being set to null

UPDATE  ae44
    SET price =
    CASE
    WHEN ID = 'AT259793380' THEN '500'
    WHEN ID = 'AT271729590' THEN '600'
    WHEN ID = 'AT275981020' THEN '700'
    WHEN ID THEN price
    END
dooplenty
  • 124
  • 7
  • 1
    +1 Cool alternative _dooplenty_, but isn't it better to restrict the update to the rows that you really want to update instead of all of them? – Armfoot Jun 29 '15 at 11:28
0

Checking the mysql error would have revealed:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= 'AT275981020' then '700' end case'

I don't know if you thought you could write case as an alias or what that alias could be used for. Maybe this was just bad copy-pasta from another CASE block that you found in a SELECT query.

Ultimately it was the trailing case that was the issue. The query will execute once that final word is removed, but there is more to explain.

In my own project, I would be using the more concise variant of CASE syntax with ID only written once instead of writing mostly redundant expressions in each case.

I also recommend using lowercase column names so that the sql is easier to read for humans.

UPDATE ae44 SET
Price = CASE ID
    WHEN 'AT259793380' THEN '500'
    WHEN 'AT271729590' THEN '600'
    WHEN 'AT275981020' THEN '700'
END
WHERE 
    ID IN ('AT259793380','AT271729590','AT275981020')

With the addition of the WHERE cause, only 3 rows will be whitelisted for possible modification -- as a consequence the ELSE can be safely omitted from the CASE block.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-2
update ae44
set Price = 
Case ID
when 'AT259793380' then '500'
when 'AT271729590' then '600'
when 'AT275981020' then '700'
end case
Meph
  • 1
  • This piece of code is a copy of the question's code with a `case ID when` syntax instead of a `case when ID =` syntax and does not answer the question, since it did not fix the syntax error for having `end case` (the last `case` was the problem) and will update all the other rows' Price field to `NULL`. – Armfoot Jul 16 '15 at 16:02