MySQL查询用下划线替换列中的空格

时间:2022-06-21 21:02:37

I have a MySQL database table 'photos' with a column 'filename'. I need to replace the spaces in the filename column values with underscores. Is it possible with a single/multiple query? If so how?

我有一个MySQL数据库表'photos',列'filename'。我需要用下划线替换filename列值中的空格。是否可以使用单个/多个查询?如果是这样的话?

2 个解决方案

#1


45  

You can use the REPLACE function :

您可以使用REPLACE功能:

REPLACE(str,from_str,to_str)

REPLACE(STR,from_str均被,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str.
REPLACE() performs a case-sensitive match when searching for from_str.

返回字符串str,其中所有出现的字符串from_str都替换为字符串to_str。搜索from_str时,REPLACE()执行区分大小写的匹配。

So, to replace all occurences of a character by another one in all lines of a table, something like this should do :

因此,要在表的所有行中替换另一个字符的所有出现,这样的事情应该:

update photos set filename = replace(filename, ' ', '_');

ie, you search for ' ' in the column filename and use '_' instead ; and put the result back into filename.

即,您在列文件名中搜索''并使用'_'代替;并将结果返回到文件名中。

#2


6  

update photos set filename = replace(filename,' ', '_');

#1


45  

You can use the REPLACE function :

您可以使用REPLACE功能:

REPLACE(str,from_str,to_str)

REPLACE(STR,from_str均被,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str.
REPLACE() performs a case-sensitive match when searching for from_str.

返回字符串str,其中所有出现的字符串from_str都替换为字符串to_str。搜索from_str时,REPLACE()执行区分大小写的匹配。

So, to replace all occurences of a character by another one in all lines of a table, something like this should do :

因此,要在表的所有行中替换另一个字符的所有出现,这样的事情应该:

update photos set filename = replace(filename, ' ', '_');

ie, you search for ' ' in the column filename and use '_' instead ; and put the result back into filename.

即,您在列文件名中搜索''并使用'_'代替;并将结果返回到文件名中。

#2


6  

update photos set filename = replace(filename,' ', '_');