代码之家  ›  专栏  ›  技术社区  ›  Bersekz

如何在MySQL中用另一个字符串和一个字段的串联替换任何字符串

  •  1
  • Bersekz  · 技术社区  · 9 年前

    我想用myString+field2的串联替换field1中的任何字符串(空的或任何东西)。我该怎么做?

    UPDATE table SET field1 = REPLACE(field1, '%', CONCAT(myString, field2));
    

    我想问题是“%”,因为我不知道如何匹配任何字符串。

    2 回复  |  直到 9 年前
        1
  •  1
  •   Gordon Linoff    9 年前

    UPDATE t  -- this is the name of the TABLE, not the COLUMN
        SET field1 = CONCAT(COALESCE(mystring, ''), COALESCE(field1, ''));
    

    这个 % 通配符仅用于 LIKE 。它不是某种通用通配符。如果要连接其他两个值:

    UPDATE t  -- this is the name of the TABLE, not the COLUMN
        SET field1 = CONCAT(COALESCE(mystring, ''), COALESCE(field2, ''));
    

    当然 COALESCE() NULL 作为空字符串(否则 CONCAT() 无效的 ).

        2
  •  0
  •   Dawud Abdul Manan    9 年前
    UPDATE table SET field1 =  CONCAT(myString, field2);
    

    函数的作用是:将两个或多个表达式连接在一起。

    CONCAT(表达式1、表达式2、表达式3,…)

     OR
    UPDATE table SET field1 = CONCAT(COALESCE(mystring, ''), COALESCE(field1, ''));
    

    欲了解更多信息 http://www.w3resource.com/mysql/comparision-functions-and-operators/coalesce-function.php