1
我試圖在UPDATE
語句的表名上設置前綴。我必須在多個數據庫上運行這個UPDATE
語句幾十次,每個數據庫都有不同的表前綴。使用MySQL用戶定義變量在UPDATE語句上連接表名稱
下面的代碼不起作用,但這是我想要完成的想法。
SET @prefix = 'prefix_';
SET @old = "old_value";
SET @new = "new_value";
UPDATE CONCAT(@prefix, 'table1') SET some_field = REPLACE(some_field, @old, @new);
UPDATE CONCAT(@prefix, 'table2') SET some_field = REPLACE(some_field, @old, @new);
UPDATE CONCAT(@prefix, 'table3') SET some_field = REPLACE(some_field, @old, @new);
書面手動將...
UPDATE prefix_table1 SET some_field = REPLACE(some_field, 'old_value', 'new_value');
UPDATE prefix_table2 SET some_field = REPLACE(some_field, 'old_value', 'new_value');
UPDATE prefix_table3 SET some_field = REPLACE(some_field, 'old_value', 'new_value');