如何用另一个字符串和 MySQL 中的一个字段的串联替换任何字符串
How to replace any string with a concatenation of another string and one field in MySQL
我想用 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()
returnsNULL
)。
UPDATE table SET field1 = CONCAT(myString, field2);
The CONCAT() function concatenates two or more expressions together.
syntax
CONCAT(expression1, expression2, expression3,...)
OR
UPDATE table SET field1 = CONCAT(COALESCE(mystring, ''), COALESCE(field1, ''));
MySQL COALESCE() function returns the first non-NULL value of a list, or NULL if there are no non-NULL values.
it will make empty '' when there is no value in mystring.
for more info http://www.w3resource.com/mysql/comparision-functions-and-operators/coalesce-function.php
我想用 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()
returnsNULL
)。
UPDATE table SET field1 = CONCAT(myString, field2);
The CONCAT() function concatenates two or more expressions together.
syntax
CONCAT(expression1, expression2, expression3,...)
OR
UPDATE table SET field1 = CONCAT(COALESCE(mystring, ''), COALESCE(field1, ''));
MySQL COALESCE() function returns the first non-NULL value of a list, or NULL if there are no non-NULL values.
it will make empty '' when there is no value in mystring. for more info http://www.w3resource.com/mysql/comparision-functions-and-operators/coalesce-function.php