MySQL update case help
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.
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
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
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)
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