List of emails, delete rows out of a table

我的未来我决定 提交于 2019-12-24 08:07:50

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/7640716/list-of-emails-delete-rows-out-of-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!