是否可以使用一个简单的查询替换SQLite数据库的所有列中的给定字符串?
假设我有一个包含四列名称,年龄,收入和朋友的表:
+------+-----+--------+---------+
| name | age | income | friends |
+------+-----+--------+---------+
| John | 20 | 10 | 0 |
+------+-----+--------+---------+
| Jane | 25 | 20 | 0 |
+------+-----+--------+---------+
| June | 20 | 40 | 20 |
+------+-----+--------+---------+
我想用一个简单的查询在整个表中用“40”替换所有“20”的实例。类似的东西:
UPDATE table SET ALL-COLUMNS = 40 WHERE ALL-COLUMNS = 20
使用SQLite可以实现吗?或者我是否必须为每列创建一个查询?
UPDATE table SET age = 40 WHERE age = 20
UPDATE table SET income = 40 WHERE income = 20
UPDATE table SET friends = 40 WHERE friends = 20
答案 0 :(得分:1)
使用三个UPDATE语句是最简单的解决方案。
可以使用单个UPDATE语句UPDATE MyTable
SET age = CASE age WHEN 20 THEN 40 ELSE age END,
income = CASE income WHEN 20 THEN 40 ELSE income END,
friends = CASE friends WHEN 20 THEN 40 ELSE friends END
WHERE age = 20
OR income = 20
OR friends = 20