MYSQL Update IF a subtring exists in another table Inner Join

落花浮王杯 提交于 2020-01-17 03:03:20

问题


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

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