测试了下面两种方式,方法一第一次运行似乎影响的行数多了几条,后面运行似乎数量又正常了,感觉有点奇怪,所以最后用了方法二,有没有能指出两个方法区别的大佬,欢迎讨论!
方法一:
UPDATE tableName
SET code = CASE desc
WHEN '条件1' THEN '0001'
WHEN '条件2' THEN '0002'
WHEN '条件3' THEN '0003'
WHEN desc NOT IN ('条件1','条件2','条件3') AND desc IS NOT NULL AND desc <> ''
THEN '0004'
END;
方法二:
UPDATE tableName SET code = '0001' WHERE desc = '条件1';
UPDATE tableName SET code = '0002' WHERE desc = '条件2';
UPDATE tableName SET code = '0003' WHERE desc = '条件3';
UPDATE tableName SET code = '0004' WHERE desc NOT IN ('条件1','条件2','条件3') AND desc IS NOT NULL AND desc <> '';