使用更正表替换列中的值

时间:2016-06-13 09:32:55

标签: mysql mysql-workbench

我正在寻找一个解决方案,使用更正表替换列中的值,以后可以添加信息。

以下是这个想法:

db.products包含需要REPLACED

的数据
materials | unique_id  |

leather   | a8sd83k2m3 |
leater    | b23k4j244w |
Coton     | 8asdh3k4er |
Plyurethan| h8dhekjnrt |

db.corrections

id | correct_value | wrong_value1 | wrong_value2 | etc..
1  | Leather       | leather      | leater       | (future miss-spellings)
1  | Cotton        | Coton        | cotonn       | (future miss-spellings)
1  | Polyurethane  | Plyurethan   | Plyurethane  | (future miss-spellings)
1  | Wool          | woool        | wo           | (future miss-spellings)

或者更快/更好的解决方案:

db.corrections

id | correct_value | wrong_value
1  | Leather       | leather,leater,(future miss-spellings)
1  | Cotton        | Coton,cotonn,(future miss-spellings)
1  | Polyurethane  | Plyurethan,Plyurethane,(future miss-spellings)
1  | Wool          | woool,wo,(future miss-spellings)

我现在使用的是UPDATE语句,其中包含多个REPLACE,但这些语句构成了很长的查询,只是一团糟,通过使用它来调用材料&#39 ; s id会更高效imho。即:

UPDATE db.product SET material = REPLACE(REPLACE(REPLACE(material,

什么是正确的方法,这样的查询将如何?我在正确的方向思考(下方)还是我需要完全不同的东西?

    db.corrections AS c
    UPDATE db.product set material = REPLACE (material, searchvalues, replacevalues) 
    WHERE searchval = c.wrong_value1 OR searchval = cwrong_value2;

我今天晚些时候会尝试更多,所以我不能比今晚更快地接受答案。非常期待你的想法。

1 个答案:

答案 0 :(得分:1)

您不必使用products加入corrections列值,只需加入所有行

id | correct_value | wrong_value
1  | Leather       | leather
2  | Leather       | leater
3  | Cotton        | Coton
4  | Cotton        | cotonn
5  | Polyurethane  | Plyurethan
6  | Polyurethane  | Plyurethane
7  | Wool          | woool
8  | Wool          | wo

 UPDATE product p, corrections c
 set p.material = REPLACE(p.material, c.wrong_value, c.correct_value);

更安全一点,因为你只想替换整个单词(例如 wo 可以是另一个单词的一部分:

UPDATE product p, corrections c
 set p.material = REPLACE(p.material, CONCAT(' ', c.wrong_value, ' '), CONCAT(' ', c.correct_value, ' '));
相关问题