You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量更新指定用户邮箱的域名部分?

批量更新邮箱域名的SQL方案

不用逐个用户编写更新语句,直接利用字符串替换函数就能批量完成邮箱域名的修改,以下是主流数据库的实现方式:

核心思路

通过数据库内置的字符串替换函数,直接将email字段中所有@test.com后缀替换为@fake.com,配合WHERE条件过滤目标邮箱,避免误改其他数据。

MySQL/MariaDB/SQL Server 实现

-- 先查询验证替换结果(可选但推荐)
SELECT users, email, REPLACE(email, '@test.com', '@fake.com') AS new_email
FROM userinfo
WHERE email LIKE '%@test.com';

-- 确认无误后执行更新
UPDATE userinfo
SET email = REPLACE(email, '@test.com', '@fake.com')
WHERE email LIKE '%@test.com';

PostgreSQL 实现

PostgreSQL同样支持REPLACE函数,也可以用正则表达式更精准匹配后缀:

-- 验证语句
SELECT users, email, REPLACE(email, '@test.com', '@fake.com') AS new_email
FROM userinfo
WHERE email LIKE '%@test.com';

-- 批量更新
UPDATE userinfo
SET email = REPLACE(email, '@test.com', '@fake.com')
WHERE email LIKE '%@test.com';

-- 正则匹配版本(更严格匹配结尾)
UPDATE userinfo
SET email = REGEXP_REPLACE(email, '@test\\.com$', '@fake.com')
WHERE email ~ '@test\\.com$';

Oracle 实现

-- 验证语句
SELECT users, email, REPLACE(email, '@test.com', '@fake.com') AS new_email
FROM userinfo
WHERE email LIKE '%@test.com';

-- 批量更新
UPDATE userinfo
SET email = REPLACE(email, '@test.com', '@fake.com')
WHERE email LIKE '%@test.com';

注意事项

  • 执行更新前一定要先运行验证语句,确认替换后的邮箱格式正确;
  • 建议先备份目标表数据,或者在测试环境完成验证后再操作生产库;
  • WHERE条件是可选的,但加上后能避免修改非@test.com结尾的邮箱,更安全。

内容的提问来源于stack exchange,提问作者Nathan1133

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 02:55:04