List of emails, delete rows out of a table - mysql

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

Related

How do I reduce the number of characters in an input string/varchar in MySQL

I basically have two tables tbl_feedback and tbl_notification that both get values inserted in them through a stored procedure whenever a person submits a feedback.
What I'm aiming for is getting a "shortened" version of a message column inserted into tbl_notification (say 60 characters max) so a notification only shows a preview of the actual message.
Is this possible using a MySQL function or do I have to resort to handling it via PHP (shortening an output string before echoing it)
The LEFT function was what I was looking for. Thanks, Paul.
I wish that google pointed me straight to this function.
w3resource on LEFT function

Query Google Admin User directory comparing parameters

I'm trying to filter my users list by comparing two parameters
query="EmployeeData.EmployeeID=externalId"
EmployeeData.EmployeeID is a custom schema that is populated, with a cron job, with the same value as externalId.
Of course I let the cron do the field copy only if necessary, this is the reason I'm trying to filtering the users list.
In the way i wrote seems that the query trying to looking for a value "externalId" into the EmployeeData.EmployeeID ignoring that "externalId" is a even a field
any suggestion?
The way your code is written, the query sent to Google's servers is as you correctly guessed the following:
EmployeeData.EmployeeID=externalId where your actual externalId is not sent but rather the string "externalId".
To replace this string for the actual value of your variable, you can use what is called "string concatenation" 1. To do it, you just need to modify your code as shown below:
query="EmployeeData.EmployeeID=" + externalId;
This way, the query will be sent as you need to Google's servers.

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'

Joomla 3x - Profile Fields

I have added a checkbox to the user profile and need to know how to get a list of all the user email addresses where they have checked the checkbox. I would like to just run a SQL query, but can't see where the value is stored.
All the profile data is stored in #__user_profiles table. To retrieve the data from the profile table you can perform this query
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all records from the user profile table where key begins with "checkbox.".
// Order it by the ordering field.
$query->select($db->quoteName(array('user_id', 'profile_key', 'profile_value')));
$query->from($db->quoteName('#__user_profiles'));
$query->where($db->quoteName('profile_key') . ' LIKE '. $db->quote('\'checkbox.%\''));
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
You will get object list of all those users who clicked on your checkbox. Remember to use the profile_key name in place of checkbox
Ok - I have now managed to answer my own question (I took an export of the database as a SQL file, ticked the checkbox as a test user, made another export and then compared the files to see what had changed).
The values are stored in the jos_fields_values table. The query below assumes that there is only one checkbox - if you have more than one you will need to query the field_id as well, Replace xxx with the prefix of your Joomla installation.
SELECT email FROM xxx_users INNER JOIN xxx_fields_values ON xxx_users.ID=xxx_fields_values.item_id

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