简体   繁体   English

如何在MySQL中运行更新查询并替换匹配的字符串

[英]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] 这就是我想要做的-MySQL数据库中的特定表[abc_tablename]的列[xyz_columnname]包含很多电子邮件ID(超过5000个),我希望它们用文本[email]替换所有电子邮件ID。 ID已删除]

I used the following query for SELECT: 我对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 我尝试了这个,但是它从xyz_columnname中删除了所有数据

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

I'm using PHPMyAdmin to run these queries. 我正在使用PHPMyAdmin运行这些查询。 Also, do note that xyz_columnname has a lot of text and numeric content along with the email ID. 另外,请注意xyz_columnname具有大量文本和数字内容以及电子邮件ID。

You can simply use this command, this will update all rows which column xyz_columnname contains an @ to show email ID Removed 您可以简单地使用此命令,这将更新xyz_columnname列中包含@所有行以显示已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. 使用单引号或双引号,否则mysql将把该值视为列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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