SQL更新和替换子字符串

23
我需要在名字表的first_name列中用SQL查询将所有字符串中的'a'替换为'b'。 以下是我的列名称: first_name | list_name
2个回答

51

使用REPLACE()函数

UPDATE tableName
SET first_name = REPLACE(first_name, 'a', 'b')

但请记住,REPLACE() 是区分大小写的。


1
https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace - feeela
有没有另一个不区分大小写的功能,或者我们可以传递给REPLACE的参数? - Francisco Corrales Morales

7
您可以尝试这个方法:
UPDATE name SET first_name = REPLACE (first_name, 'a', 'b') WHERE blabla LIKE '%blabla%';
OR
UPDATE name SET first_name = REPLACE (first_name, 'a', 'b') WHERE blabla = 'blabla';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接