一尘不染

MySQL更新案例帮助

mysql

有人可以告诉我如何解决此查询吗?

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

我只想通过匹配ID来大规模更新基于ID的挂牌价格。


阅读 354

收藏
2020-05-17

共1个答案

一尘不染

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

请注意,此查询将使用其他ID将记录的价格更新为NULL

如果只想更新列表中的ID,请使用以下命令:

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
2020-05-17