简体   繁体   中英

How do I run Update Query in MySQL and replace a matching string

I tried a couple of things but it deleted the complete content from the column.

Here's what I want to do - A particular table [abc_tablename] in my MySQL database has a column [xyz_columnname] that has a lot of email IDs (more than 5000) and I want them to replace all the email IDs with a text [email ID removed]

I used the following query for SELECT:

SELECT * 
  FROM abc_tablename
 WHERE xyz_columnname LIKE '%@%'

How do I perform the replace function in this case? Can you help me with the query for the same. I tried this but it removed all the data from xyz_columnname

UPDATE abc_tablename 
   SET xyz_columnname = REPLACE (  xyz_columnname,  LIKE ''%@%'',  '**email ID removed**');

I'm using PHPMyAdmin to run these queries. Also, do note that xyz_columnname has a lot of text and numeric content along with the email ID.

You can simply use this command, this will update all rows which column xyz_columnname contains an @ to show email ID Removed

UPDATE abc_tablename 
    SET `xyz_columnname` = 'email ID removed'
    WHERE `xyz_columnname` LIKE '%@%'

Use ` (backquotes) for column name

Use ' or " for values

Don't use backticks with column values. use either single or double quotes otherwise mysql will consider that value as a column name.

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