Replace a word in BLOB text by MySQL

匆匆过客 提交于 2019-12-18 16:53:26

问题


I've got a huge mysql table (called tcountriesnew) and a column (called slogen, blob type). In each of those slogen blobs I'd like to replace a word, for example: banana to apple.

Unfortunately I tried to print all the rows with word banana, and it did not work.

select * from tcountriesnew where slogen like '%banana%';

Please help me.

  • What i missed, what is the problem with my query?
  • How can i replace text in blob?

回答1:


Depends what you mean by "replace" show in select:

select replace(slogen, 'bananas', 'apples') from tcountriesnew where slogen like '%bananas%';

Or update data in a table:

update tcountriesnew set slogen=replace(slogen, 'bananas', 'apples') where slogen like '%bananas%';

BTW. Why are you using blob for text? You should use text type for textual data and blob for binary data.




回答2:


In some cases it is needed to save texts BLOB. In my case, for whatever reason Drupal 7 developers choose to use a blob for all TEXT columns - this is out of developer's control.

To convert a blob to text, use the MySql convert function. Then you have to save it into the database and convert it again to blob - but this is handled automatically by MySQL. So the following query would do the trick:

UPDATE tcountriesnew 
SET slogen = replace(CONVERT(slogen USING utf8), 'bananas', 'apples')
WHERE slogen LIKE '%bananas%';

On MySQL 5.5, This resolved my issue completely.

Also, configure your PhpMyAdmin to show Blob data




回答3:


Which version are you using ? Maybe it's this bug : http://bugs.mysql.com/bug.php?id=27. Otherwise try to cast your blob column.



来源:https://stackoverflow.com/questions/3966174/replace-a-word-in-blob-text-by-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!