简体   繁体   English

搜索并替换部分字符串

[英]Search and replace part of string

I would like to know how to replace the a part of a value which is stored into a columns named "keyword_data" in the table "keyword". 我想知道如何替换存储在表“ keyword”中名为“ keyword_data”的列中的值的一部分。

The values in the MySQL columns look like this: MySQL列中的值如下所示:

a:5:{s:11:"{%KEYWORD%}";s:59:"keyword test 1";s:9:"{%PRICE%}";s:6:"279,99";s:12:"{%CATEGORY%}";s:6:"Camera";s:9:"{%IMAGE%}";s:61:"http://exemple.com/images/I/1172oJo2lpL._AA160_.jpg";s:12:"{%REDIRECT%}";s:158:"http://www.exemple.net/keywordtest1/dp/B00EHK7QVK/ref=sr_1_750?s=hi&ie=UTF8&qid=1428056859&sr=1-750&keywords=hd1?tag=mytag";}

a:5:{s:11:"{%KEYWORD%}";s:53:"keyword test 2";s:9:"{%PRICE%}";s:6:"397,61";s:12:"{%CATEGORY%}";s:3:"cat";s:9:"{%IMAGE%}";s:61:"exemple.com/images/I/21iYVLu80SL._AA160_.jpg";s:12:"{%REDIRECT%}";s:187:"exemple.com/keywordtest2/dp/B00RDF1GLW/ref=sr_1_6064?s=industrial&ie=UTF8&qid=1428944582&sr=1-6064&keywords=hd2?tag=mytag";}

etc. 等等

I cannot just make a simple replace value since they are all different. 我不能仅仅给出一个简单的替换值,因为它们都是不同的。

Each row has a different keyword, I only need to replace this for each row in the table. 每行都有一个不同的关键字,我只需要为表中的每一行替换它。

 ?tag=mytag

into

 &tag=mytag

Any help from the community would be cool :) 来自社区的任何帮助都将很酷:)

mysql provides a command called REPLACE. mysql提供了一个名为REPLACE的命令。

REPLACE has the following structure: REPLACE具有以下结构:

 REPLACE(text, from_string, to_string)

This can be used in an update statement: 可以在更新语句中使用:

    UPDATE keyword SET keyword_data = REPLACE(keyword_data, '?tag=mytag', '&tag=mytag');

You can use REPLACE() https://msdn.microsoft.com/en-us/library/ms186862.aspx 您可以使用REPLACE() https://msdn.microsoft.com/en-us/library/ms186862.aspx

SELECT REPLACE('?tag=mytag','?','&');
->&tag=mytag

Just do: 做就是了:

  UPDATE keyword SET keyword_data = REPLACE( keyword_data , '?tag=mytag' , '&tag=mytag') 
         WHERE keyword_data LIKE '%?tag=mytag%';

Working example! 工作示例!

Also you should be really careful with this. 另外,您应该对此非常小心。 Always make a copy of your database before you fool around with it like that since you can easily mess everything up just because you missed a little char somewhere. 像这样愚弄数据库之前,请务必先对其进行备份,因为您可能会因为在某个地方错过了一个小字符而轻易地弄乱了所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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