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的上市价格。
UPDATE ae44 SET price = CASE WHEN ID = 'AT259793380' THEN '500' WHEN ID = 'AT271729590' THEN '600' WHEN ID = 'AT275981020' THEN '700' END
请注意,此查询会将其他id的logging的价格更新为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
UPDATE ae44 SET Price = CASE WHEN ID = 'AT259793380' THEN '500' WHEN ID = 'AT271729590' THEN '600' WHEN ID = 'AT275981020' THEN '700' ELSE Price END
或者你可以使用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')
并设置极限也是好主意:
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
删除第二个“案例”,它将工作:
UPDATE ae44 SET Price = (CASE WHEN ID = 'AT259793380' THEN '500' WHEN ID = 'AT271729590' THEN '600' WHEN ID = 'AT275981020' THEN '700' END)
我假设这是一个MySQL查询。 您可以使用Quassnoi发布的第一个查询并添加
WHEN ID THEN price
作为最后的“当”。 这将防止您的所有价格字段被设置为空
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
你可以尝试一个简单的查询,如:
UPDATE `table` SET Price = ELT(field(ID,'AT259793380','AT271729590','AT275981020'), '500', '600', '700') WHERE ID IN ('AT259793380','AT271729590','AT275981020')
update ae44 set Price = Case ID when 'AT259793380' then '500' when 'AT271729590' then '600' when 'AT275981020' then '700' end case