简体   繁体   English

如果另一个表中存在子控件,则进行MYSQL更新

[英]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. 我想做的是“内部连接”到另一个表,称为“ 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. 我可以将其写为PHP的循环,但我想在这里问社区是否有人在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. emailCarriers表包含所有错误的域运营商,因此查询将引用emailCarriers表,并尝试在域名部分的子字符串上找到匹配项(在@符号之后),如果匹配,则将执行更新并填充在“ bad-domain.com”中,并带有emailCarriers表中的相应域。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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