[英]MySQL query to replace spaces in a column with underscores
I have a MySQL database table 'photos' with a column 'filename'. 我有一个MySQL数据库表'photos',列'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?
如果是这样的话?
You can use the REPLACE
function : 您可以使用
REPLACE
功能:
REPLACE(str,from_str,to_str)
Returns the string
str
with all occurrences of the stringfrom_str
replaced by the stringto_str
.返回字符串
str
其中所有出现的字符串from_str
替换为字符串to_str
。
REPLACE()
performs a case-sensitive match when searching forfrom_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 ; 即,您在列
filename
搜索''并使用'_'代替; and put the result back into filename
. 并将结果返回到
filename
。
update photos set filename = replace(filename,' ', '_');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.