Mysql-如何更新“domain.com”在“address@domain.com”中

发布于 2024-09-03 09:21:08 字数 472 浏览 2 评论 0原文

在我的数据库中,有很多用户的电子邮件地址拼写错误。这反过来又导致我的 postfix 在发送时事通讯时退回大量邮件。
形式包括(但不限于)“yaho.com”、“yahho.com”等
很烦人!

所以我一直在尝试将这些记录更新为正确的值。
执行 select email from users where email like '%@yaho%' and email not like '%yahoo%'; 并获取列表后,我陷入困境,因为我不知道如何仅更新yaho 部分。我需要完整保留用户名。

所以我想我应该转储数据库并使用 vim 替换,但我无法转义 @ 符号。

顺便说一句,如何选择所有用大写字母写的电子邮件地址? select upper(email) from users; 只会将所有内容转换为大写字母,而我只需要找出已经写入大写字母的邮件。

In my database I have a lot of users who've misspelled their e-mail address. This in turn causes my postfix to bounce a lot of mails when sending the newsletter.
Forms include (but are not limited to) "yaho.com", "yahho .com" etc.
Very annoying!

So i have been trying to update those record to the correct value.
After executing select email from users where email like '%@yaho%' and email not like '%yahoo%'; and getting the list, I'm stuck because I do not know how to update only the yaho part. I need the username to be left intact.

So I thought I would just dump the database and use vim to replace, but I cannot escape the @ symbol..

BTW, how do I select all email addresses written in CAPS? select upper(email) from users; would just transform everything into CAPS, whereas I just needed to find out the already-written-in-CAPS mails.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千紇 2024-09-10 09:21:08

您可能想尝试如下操作:

UPDATE   users
SET      email = CONCAT(LEFT(email, INSTR(email, '@')), 'yahoo.com')
WHERE    email LIKE '%@yaho.com%';

测试用例:

CREATE TABLE users (email varchar(50));

INSERT INTO users VALUES ('[email protected]');
INSERT INTO users VALUES ('[email protected]');
INSERT INTO users VALUES ('[email protected]');


UPDATE   users
SET      email = CONCAT(LEFT(email, INSTR(email, '@')), 'yahoo.com')
WHERE    email LIKE '%@yaho.com%';

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0


SELECT * FROM users;
+-----------------+
| email           |
+-----------------+
| [email protected] |
| [email protected] |
| [email protected] |
+-----------------+
3 rows in set (0.00 sec)

要回答第二个问题,您可能需要 使用区分大小写的排序规则,例如 latin1_general_cs

SELECT * FROM users WHERE email COLLATE latin1_general_cs = UPPER(email);

测试用例:

INSERT INTO users VALUES ('[email protected]');


SELECT * FROM users;   
+-----------------+
| email           |
+-----------------+
| [email protected] |
| [email protected] |
| [email protected] |
| [email protected] |
+-----------------+
4 rows in set (0.00 sec)


SELECT * FROM users WHERE email COLLATE latin1_general_cs = UPPER(email);
+-----------------+
| email           |
+-----------------+
| [email protected] |
+-----------------+
1 row in set (0.00 sec)

You may want to try something like the following:

UPDATE   users
SET      email = CONCAT(LEFT(email, INSTR(email, '@')), 'yahoo.com')
WHERE    email LIKE '%@yaho.com%';

Test case:

CREATE TABLE users (email varchar(50));

INSERT INTO users VALUES ('[email protected]');
INSERT INTO users VALUES ('[email protected]');
INSERT INTO users VALUES ('[email protected]');


UPDATE   users
SET      email = CONCAT(LEFT(email, INSTR(email, '@')), 'yahoo.com')
WHERE    email LIKE '%@yaho.com%';

Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0


SELECT * FROM users;
+-----------------+
| email           |
+-----------------+
| [email protected] |
| [email protected] |
| [email protected] |
+-----------------+
3 rows in set (0.00 sec)

To answer your second question, you probably need to use a case sensitive collation such as the latin1_general_cs:

SELECT * FROM users WHERE email COLLATE latin1_general_cs = UPPER(email);

Test case:

INSERT INTO users VALUES ('[email protected]');


SELECT * FROM users;   
+-----------------+
| email           |
+-----------------+
| [email protected] |
| [email protected] |
| [email protected] |
| [email protected] |
+-----------------+
4 rows in set (0.00 sec)


SELECT * FROM users WHERE email COLLATE latin1_general_cs = UPPER(email);
+-----------------+
| email           |
+-----------------+
| [email protected] |
+-----------------+
1 row in set (0.00 sec)
又爬满兰若 2024-09-10 09:21:08
UPDATE users SET email = REPLACE( email, SUBSTRING_INDEX( email,  '@', -1 ) ,  CONCAT(user_id, 'domain.com' ) ) WHERE [MYSQL CONDITION];

将实时电子邮件地址更新为测试电子邮件地址

UPDATE users SET email = REPLACE( email, SUBSTRING_INDEX( email,  '@', -1 ) ,  CONCAT(user_id, 'domain.com' ) ) WHERE [MYSQL CONDITION];

To update live email address to test email address

氛圍 2024-09-10 09:21:08

为了解决你的第二个问题(关于查找大写字母写的电子邮件),这样的事情可能会有所帮助:(

select email from users where upper(email) = email

如果语法不完全正确,请原谅我,因为我习惯了 DB2。这个想法是将直接电子邮件地址与大写版本。)

To address your second question (about finding emails written in caps), something like this might be helpful:

select email from users where upper(email) = email

(Forgive me if the syntax is not precisely correct, since I'm used to DB2. The idea is to compare the straight email address with the upper-cased version.)

怀念你的温柔 2024-09-10 09:21:08
UPDATE contacts SET email = REPLACE(email, SUBSTRING_INDEX(email, '@', -1), 'domain.com')
UPDATE contacts SET email = REPLACE(email, SUBSTRING_INDEX(email, '@', -1), 'domain.com')
旧街凉风 2024-09-10 09:21:08

您可以尝试将 INSTRSUBSTRLEFT 一起使用来获取“@”符号之前的部分。

SELECT LEFT("[email protected]",INSTR ("[电子邮件受保护]","@")-1); 似乎有效。

You could try using INSTR together with SUBSTR or LEFT to get the part before the "@" symbol, perhaps.

Something like SELECT LEFT("[email protected]",INSTR("[email protected]","@")-1); seems to work.

掩饰不了的爱 2024-09-10 09:21:08

对于第一个问题,我会选择类似的东西

UPDATE users
SET email = INSERT(email,INSTR(email,'@'), LENGTH(email), '@yahoo.com')
WHERE email LIKE '%@yaho.com'

只是为了彻底,这是多字节安全的,即使我使用了LENGTH。所需要的只是 INSERT 的第三个参数至少与子字符串的末尾一样大。

语法找到全大写电子邮件的答案是一个很好的答案。尽管您可能不会注意到差异,但执行速度可能稍快一些,但

SELECT email FROM users WHERE BINARY(email) NOT REGEXP '[a-z]'

需要更新:BINARY(email) 来强制区分大小写的匹配。

For the first question, I would choose something like

UPDATE users
SET email = INSERT(email,INSTR(email,'@'), LENGTH(email), '@yahoo.com')
WHERE email LIKE '%@yaho.com'

Just for the sake of being thorough, this is multi-byte safe even though I've used LENGTH. All that's needed is for the third argument of INSERT to be at least as large as the end of the substring.

Syntactic's answer of finding the all-caps email is a good answer. Possibly performing slightly faster, although you would likely not notice the difference, is

SELECT email FROM users WHERE BINARY(email) NOT REGEXP '[a-z]'

Update: BINARY(email) is needed to force case-sensitive matching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文