use csv list to update a MySQL field - mysql

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.

Related

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'

MySQL change email domains

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;

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%'

MySQL Query - Check if data row contains a user_id

I have a sessions table and what I am trying to do is run a query to check if a certain account is logged in.
My query looks like this:
SELECT id FROM sessions WHERE data LIKE '%4%'
But with a query like that, if the user_id of "14" is logged in, that query will return True. How do I check to see if a certain number exists and matches exactly?
Include the delimiters in your search:
WHERE data LIKE '%:4;%'
why don't you add another field for user_id to the session table?
EXAMPLE:
I have a site where a user can only be logged in from one location at a time. If they try and log in from another location (IE start a new session) then i kill all of their previous logins.
So as part of my login script:
// log this user out of any other active sessions
$sql = sprintf("DELETE
FROM sessions
WHERE id_user=%s",
mysql_real_escape_string($_SESSION['id_user'])
);
// Associate this session with this user
$sql = sprintf("UPDATE sessions
SET id_user=%s
WHERE id=%s",
mysql_real_escape_string($_SESSION['id_user']),
session_id()
);
and so i can have id_user as an additional field in my session FKed to my user table... its a little more normalized and lets you do quick "Who is currently using this site" queries without too much parsing and fuss.

List of emails, delete rows out of a table

If I have a list of of email accounts in a .txt file, is there a way I can perform a mysql delete statement for each instance of the rows that contain the email account against a table?
We have a newsletter mailing list which around 600 emails are currently invalid, and we want an easier way of getting rid of them besides manually going in one by one to do it.
Use excel to turn the dataset into a comma separated string and then simply:
DELETE FROM YOUR_TABLE
WHERE email IN ('example#aol.com', 'example2#aol.com')
Note that you will need to manually add the single quotes before and after each email address in excel as well.
Assuming you want to use the text file's contents as the source of addresses to delete from the database:
$addreses = file('emails.txt');
foreach($addresses as $address) {
$safe_address = mysql_real_escape_string($address);
$sql = "DELETE FROM yourtable WHERE (email = '$safe_address');";
$result = mysql_query($sql) or die(mysql_error());
}
is the simplest form. You can do some things to optimize the process, such as creating a list of emails in quoted/comma-separated form and using WHERE IN (...) instead, to reduce the number of queries generated/executed.
Note that I'm using PHP as a pseudo-code placeholder, but the basic principle would be the same in pretty much any language.
What about create the delete statement for single email then use Microsoft excel to complete the rest of the emails? by using the concatenate function and matching with the list of the selected emails