我有一张这样的表: -
Product ID Weight A 100 B 100 C 100 D 100 E 100
我想将其更改为: -
Product ID Weight A 501 B 601 C 701 D 801 E 401
如何使用SQL更新命令?
答案 0 :(得分:8)
像这样使用案例表达
UPDATE products SET
Weight =
CASE ProductID
WHEN 'A' THEN 501
WHEN 'B' THEN 601
WHEN 'C' THEN 701
WHEN 'D' THEN 801
WHEN 'E' THEN 401
END
WHERE ProductID in ('A', 'B', 'C', 'D', 'E')
如果没有WHERE子句,表中的每一行都将被测试(不必要的,因为它们永远不会匹配)。
答案 1 :(得分:0)
UPDATE [TABLE] SET Weight = 501 WHERE [Product ID] = 'A'
GO
UPDATE [TABLE] SET Weight = 601 WHERE [Product ID] = 'B'
GO
UPDATE [TABLE] SET Weight = 701 WHERE [Product ID] = 'C'
GO
UPDATE [TABLE] SET Weight = 801 WHERE [Product ID] = 'D'
GO
UPDATE [TABLE] SET Weight = 401 WHERE [Product ID] = 'E'
GO