MySQL change email domains - mysql

I am using benkeens open source script (http://benkeen.github.io/generatedata/) to generate random data. The problem is it gives weird email domains within the database. The code I was using to change the domains is:
UPDATE emails
SET email_address = CONCAT(LEFT(email_address, INSTR(column, '#')), 'google.com')
WHERE email LIKE '%#magnamalesuada.org%';
The only issue is, there are literally a hundred different domains being generated. Is it possible to change domain names to google, yahoo, hotmail, etc in a more efficient way?

Assuming you have an id column, you could use the % operator to divide your rows in groups, for example in 3:
UPDATE emails
SET email_address = CONCAT(LEFT(email_address, INSTR(column, '#')), 'google.com')
WHERE id%3=0;
UPDATE emails
SET email_address = CONCAT(LEFT(email_address, INSTR(column, '#')), 'yahoo.com')
WHERE id%3=1;
UPDATE emails
SET email_address = CONCAT(LEFT(email_address, INSTR(column, '#')), 'hotmail.com')
WHERE id%3=2;

Related

Modify exiting email instead of making a new one

I should modify the existing email by discarding everything after # from the existing email address (or taking only the initial part of the email address to the # sign) and adding iths.edu.rs.
This one is wrong because it creates new email.
SELECT fname,lname,email,CONCAT(LOWER(fname),'.',LOWER(lname), '#iths.edu.rs') AS 'new_email'
FROM employee
WHERE
(LEFT(fname,1)='s' OR LEFT(lname,1)='s')
AND email IS NOT NULL
This is what I get
These are expected results
I think that you want to update the table:
UPDATE employee
SET email = CONCAT(LOWER(fname),'.',LOWER(lname), '#iths.edu.rs')
WHERE (LEFT(fname,1)='s' OR LEFT(lname,1)='s') AND email IS NOT NULL
I'm not sure about the condition:
(LEFT(fname,1)='s' OR LEFT(lname,1)='s')
but I left it there because you use it in your code.
I ended up using this to show only people whose emails have been changed. This was my task.
UPDATE employee
SET email = CONCAT(LOWER(fname),'.',LOWER(lname), '#iths.edu.rs')
WHERE (LEFT(fname,1)='s' OR LEFT(lname,1)='s') AND email IS NOT NULL;
SELECT fname,lname,email
FROM employee
WHERE (LEFT(fname,1)='s' OR LEFT(lname,1)='s') AND email IS NOT NULL

Update Column from another column in same table

we need to update our mail addresses in a table, because we have a new domain-part. The local-part remains untouched
In the table is a column for Name, Surname and Mail. (And other columns which are not important).
We want it to look like this in the end:
Name Surname Mail
Test Name Test.Name#newdomain.com
Test2 Name2 Test2.Name2#newdomain.com
But while trying to do so we broke it and now the mail column only shows the new domain. We used the following code:
update table
set mail = Replace('olddomain.com','newdomain.com')
where mail LIKE '%olddomain.com'
So now we need to restore the mail column and add the new domain-part. Any help?
I'm surprised this works. Normally, replace() takes three arguments:
set mail = Replace(mail, 'olddomain.com', 'newdomain.com')
I might suggest that you include the # in the logic as well.
replace() takes three arguments
update table
set mail = Replace(mail,'#olddomain.com','#newdomain.com')
where mail LIKE '%olddomain.com'

use csv list to update a MySQL field

I have csv list which contains bounced emails addresses, one per line.
example1#example1.com
example2#example2.com
example3#example2.com
...
example700#example12.com
I need to change the user_state for each of these email to 'email_bounce'.
With the following SQL query i can do the changes using one email address at a time.
UPDATE xf_user SET user_state = 'email_bounce' WHERE email = 'example1#example1.com';
UPDATE xf_user SET user_state = 'email_bounce' WHERE email = 'example2#example2.com';
and so on
I made a copy of the DB where i would like to do a dry run but i have no idea how to automate this SQL query to go through all the emails in my csv.

UPDATING partial string from a field mysql

I have a table with columns like:
emails, country_code etc
Some of the rows contain emails like:
XXXXX#googlemail.com
I want to change about 10,000 records like this in a way that will affect only the "googlemail.com" part of the value and change all of them to "gmail.com".
UPDATE exmple_table SET emails = REPLACE(emails, '%googlemail.com','%gmail.com');
I tried to find and replace but that making me have to type all 10,000 addresses in the query, any solutions?
You can use 'like' operator to filter out the records which contain 'googlemail' and then perform the string replace on them, as shown below:
update table
set SET emails = REPLACE(emails, 'googlemail.com','gmail.com')
where emails like '%googlemail.com%'

Updating mysql email field without affecting

Recently I mass added customers details. However, my client just updated me that the email address format is wrong.
For example: The previous given email was abc#mail.com. However, I have to edit to this to abc#me.mail.com.
Is there any way I can check for #mail.com and change it to #me.mail.com?
update your_table
set email = replace(email, '#mail.com', '#me.mail.com')
where email like '%#mail.com'