简体   繁体   中英

Find and Replace entire entries in the entire table using a MySQL query

Right now Im using the following script to find and replace text in a MySQL database using phpmyadmin and it works fine.

For a single table update

 UPDATE 'table_name'
 SET 'field_name' = replace(same_field_name, 'unwanted_text', 'wanted_text')

What I want now is to find entries that contains a specified string, and then replace the whole field.

So for example, If I have the following entries:

ABC_1234
ABC_123456789
XYZ_1234

And I want to look for the entries that contains "ABC_" and replace the entire field to "FGH-432"

The result of the desired script will give the following output.

ABC_1234 > FGH-432
ABC_123456789 > FGH-432
XYZ_1234 (No change)

What modification should I make to the script?

You could do:

UPDATE `table_name`
   SET `field_name` = 'FGH-432'
 WHERE `field_name` LIKE 'ABC\_%';

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