MySQL query to replace spaces in a column with underscores
I have a MySQL database table 'photos' with a column 'filename'. I need to replace the spac开发者_如何学编程es 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(str,from_str,to_str)
Returns the string
str
with all occurrences of the stringfrom_str
replaced by the stringto_str
.REPLACE()
performs a case-sensitive match when searching forfrom_str
.
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
.
update photos set filename = replace(filename,' ', '_');
精彩评论