John Davidson

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

0 comments
Message:


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?



My Answer:
"ABC" and replace the entire entry with "NEW_ENTRY", the query would be:

UPDATE table_name SET field_name = 'NEW_ENTRY' WHERE field_name LIKE '%ABC%';

This query will find all entries in the field_name column that contain "ABC" and replace the entire entry with "NEW_ENTRY".

Rate this post

5 of 5 based on 3788 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us