MySQL String Manipulations:更改第5个字符,从结尾开始计算

时间:2012-08-29 12:18:38

标签: mysql sql

我需要更改最后一列中的第5个字符。

例如,

  • 路径/的/图像/ 1232323 _ m。JPG

  • 路径/的/图像/ 1232323 _ b。JPG

如何使用MySQL查询?

3 个答案:

答案 0 :(得分:4)

update your_table
 set col = concat(substring(col, 1, length(col) -5), 'b', substring(col, -4))
where length(col) > 5  --if you really need.

http://sqlfiddle.com/#!2/31774/2

答案 1 :(得分:1)

怎么样

update your_table
set col = replace(col, 'm.jpg', 'b.jpg')

答案 2 :(得分:1)

检查

select col, if(length(col)<5,col,concat(substr(col,1,length(col)-5),'X',substr(col,length(col)-3,5))) from table;

并基于它运行更新:

update table set col=concat(substr(col,1,length(col)-5),'X',substr(col,length(col)-3,5)) where length(col)>5;

直播示例:http://sqlfiddle.com/#!2/dbc21/2/0