问题
What I'm working on is an email clean up script for our database. In so doing we identified a list of domains that are invalid, broken or no longer around. We do this by identifying the domain name i.e. everything after the @ sign.
update url_links
set link_bad=1,
emailCarrier='bad-domain.com'
where contact_email like '%@bad-domain.com';
identifies the provider and sets the field. The problem is we have hundreds of domains that are not valid (the above is just an example)
What I'd like to do is 'inner join' to another table that is called 'emailCarriers. I could write this as a loop in PHP but I wanted to ask the community here if someone had a clever way to do this in MySQL.
The emailCarriers table contains all the bad domain carriers so the query would reference the emailCarriers table and seek to find a match on the substring of the domain name portion (after the @ sign) and if its a match then it would perform the update and fill in the 'bad-domain.com' with the corresponding domain from the emailCarriers table.
Thanks!
回答1:
update url_links ul
join emailCarriers ec on ul.contact_email like concat('%@', ec.domain)
set ul.link_bad=1,
ul.emailCarrier=ec.domain;
回答2:
You can try something like this:-
UPDATE url_links u JOIN emailCarrier e
ON u.SUBSTRING_INDEX(url_links, '@', 1) = b.provider
SET link_bad = 1
来源:https://stackoverflow.com/questions/39578487/mysql-update-if-a-subtring-exists-in-another-table-inner-join