查找行号,加1,然后替换字符串

时间:2019-05-04 15:11:31

标签: mysql

我有与此类似的行

a:1:{s:5:"value";s:69:"https://www.mypage.com/files/products/product.jpg";}

我想提取数字69,加1并用

替换整个字符串

a:1:{s:5:"value";s:70:"https://www.mypage.com/files/products/product.jpg";}

如何处理?这可以在mysql中完成吗?

我使用mysql 5.5。

谢谢。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情...

merge

要更新数据,请执行此操作。

df1.merge(df2, left_on='t', right_on='t')

逐步查询

步骤01

SELECT REPLACE(col, Substring_index(Substring_index(col, '"value";s:', -1), ':"http', 1),
              Cast(Substring_index(Substring_index(col, '"value";s:', -1), ':"http', 1) AS UNSIGNED) + 1) AS col
FROM   TableName 

步骤02-获取值

UPDATE TableName
SET    col = REPLACE(col, Substring_index(Substring_index(col, '"value";s:', -1), ':"http', 1),
                          Cast(Substring_index(Substring_index(col, '"value";s:', -1), ':"http', 1) AS UNSIGNED) + 1);

步骤03-添加+1

SELECT *
FROM   TableName

+-----------------------------------------------------------------------------+
|                                    col                                      |
+-----------------------------------------------------------------------------+
| a:1:{s:5:"value";s:69:"https://www.mypage.com/files/products/product.jpg";} |
+-----------------------------------------------------------------------------+

步骤04-将值替换为新值

SELECT Substring_index(Substring_index(col, '"value";s:', -1), ':"http', 1)
FROM   TableName 

+-----------------------------------------------------------------------+
| Substring_index(Substring_index(col, '"value";s:', -1), ':"http', 1)  |
+-----------------------------------------------------------------------+
|                                                                    69 |
+-----------------------------------------------------------------------+

在线演示:https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=9df7f1b01e7a8fa33ccc1c222dd3b411

相关问题