我想用 myString + field2 的串联替换 field1 中的任何字符串(空或其他)。我该怎么做?
这是我尝试过的,但它不起作用:
UPDATE table SET field1 = REPLACE(field1, '%', CONCAT(myString, field2));
我猜问题是“%”,因为我不知道如何匹配任何字符串。
最佳答案
你会这样做:
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()
返回 NULL
).
关于mysql - 如何用 MySQL 中另一个字符串和一个字段的串联替换任何字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45388841/