简体   繁体   English

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

[英]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 string from_str replaced by the string to_str . 返回字符串str其中所有出现的字符串from_str替换为字符串to_str
REPLACE() performs a case-sensitive match when searching for from_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM