简体   繁体   中英

MYSQL Update IF a subtring exists in another table Inner Join

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 ie 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!

update url_links ul
join emailCarriers ec on ul.contact_email like concat('%@', ec.domain)
set ul.link_bad=1,
    ul.emailCarrier=ec.domain;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM