Can MySQL replace multiple characters?

前端 未结 6 2080
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 05:42

I\'m trying to replace a bunch of characters in a MySQL field. I know the REPLACE function but that only replaces one string at a time. I can\'t see any appropriate function

6条回答
  •  日久生厌
    2020-11-27 06:25

    on php

    $dataToReplace = [1 => 'one', 2 => 'two', 3 => 'three'];
    $sqlReplace = '';
    foreach ($dataToReplace as $key => $val) {
        $sqlReplace = 'REPLACE(' . ($sqlReplace ? $sqlReplace : 'replace_field') . ', "' . $key . '", "' . $val . '")';
    }
    echo $sqlReplace;
    

    result

    REPLACE(
        REPLACE(
            REPLACE(replace_field, "1", "one"),
        "2", "two"),
    "3", "three");
    

提交回复
热议问题