简体   繁体   中英

How to find and replace string in mysql query

I have something like this

No. | Incorrect_Keyword | Correct Keyword

1 | flash | Flash Player

2 | flash player | Flash Player

3 | flash player plugin | Flash Player

4 | flash player plgin | Flash Player

It's saved in csv file and I use PHP to read each line and replace

The problem is when I run query

$query="UPDATE keyword SET keyword= REPLACE(keyword, 'incorrect_keyword',  'correct_keyword') WHERE INSTR(keyword, 'incorrect_keyword') > 0"; 

It replace all the "flash" to "Flash Player" So record number 2 will become "FLash Player player" How to make it right? Thanks

如果您要将所有Flash值都删除到Flash Player中,则可以简单地使用like查询。

UPDATE `keyword` set `keyword` = 'Flash Player' where `keyword` like '%flash%';

You have to use LIKE instead of INSTR

So your query must be something like below

UPDATE keyword SET keyword= REPLACE(keyword, 'incorrect_keyword',  'correct_keyword') WHERE `keyword` LIKE CONCAT('%',`incorrect_keyword`,'%');

supposed incorrect_keyword and keyword are column names

为更新添加一个ORDER BY:

$query="UPDATE keyword SET keyword= REPLACE(keyword, `incorrect_keyword`,  `correct_keyword`) WHERE INSTR(keyword, 'incorrect_keyword') > 0" ORDER BY No DESC; 

Some php equivalent of ucwords is here.

https://www.thingy-ma-jig.co.uk/blog/30-09-2010/mysql-how-upper-case-words

A bit tedious but should be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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