访问SQL查询多个更新

时间:2017-10-24 09:36:02

标签: sql ms-access

希望你能帮助我理解我的想法:

我有table包含数据并创建了一些查询(大约10-15)来更新fruitclass字段:

UPDATE [tblFruit] SET fruitclass = "Apple"
WHERE Fruit_Name Like "*apple*";

'this is a separate query
UPDATE [tblFruit] SET fruitclass = "Orange"
WHERE Fruit_Name Like "*orange*";

依此类推......

那么现在,想问一下是否可以将这些多个查询合并为一个?或者我会坚持单独的查询?谢谢!

1 个答案:

答案 0 :(得分:0)

考虑使用IIF()条件逻辑表达式:

UPDATE [tblFruit] SET fruitclass = IIF(Fruit_Name Like '*apple*', 'Apple', NULL),
                      fruitclass = IIF(Fruit_Name Like '*orange*', 'Orange', NULL),
                       -- ...add comma-separated expressions
;

或嵌套IIF(),但可能会弄乱10-15个表达式:

UPDATE [tblFruit] SET fruitclass = IIF(Fruit_Name Like '*apple*', 'Apple',
                                       IIF(Fruit_Name Like '*orange*', 'Orange', NULL))
;