且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MYSQL更新IF另一个表中存在一个副本Inner Join

更新时间:2022-12-12 07:46:29

 更新url_links ul 
在ul.contact_email上加入emailCarriers ec,如concat ('%@',ec.domain)
set ul.link_bad = 1,
ul.emailCarrier = ec.domain;


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!

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