我正在使用包含一些更新的脚本来更改数据库(oracle),如下所示:
UPDATE customer SET status = REPLACE(status, 'X_Y', 'xy') WHERE status LIKE '%X_Y%' AND category_id IN (SELECT id FROM category WHERE code = 'ABC'); UPDATE customer SET status = REPLACE(status, 'X_Z', 'xz') WHERE status LIKE '%X_Z%' AND category_id IN (SELECT id FROM category WHERE code = 'ABC'); -- More updates looking the same...
在这种情况下,你将如何强制干(不要重复自己)?
我特别感兴趣的是解决以下两个反复出现的问题:
定义仅从此脚本可用的函数,以提取子查询 SELECT id FROM category WHERE code = 'ABC'
创建一组替换规则(可能看起来像{"X_Y": "yx", "X_Z": "xz", ...}
一种流行的编程语言),然后在其上迭代单个更新查询.
谢谢!
我会把它减少到一个查询:
UPDATE customer SET status = REPLACE(REPLACE(status, 'X_Y', 'xy'), 'X_Z', 'xz') WHERE status REGEXP_LIKE 'X_[YZ]' AND category_id IN (SELECT id FROM category WHERE code = 'ABC');