Mysql 仅选择第二个表中不存在的记录

Mysql 仅选择第二个表中不存在的记录,mysql,sql,join,left-join,inner-join,Mysql,Sql,Join,Left Join,Inner Join,我有一个mysql table1,其中包含hostname字段和以下值: HostName ----- sa77.com ca77cded.com sa65yacd.com ca65zededs.com sa88y.com sa99ujk8.com 我还有另一个表2,它有name字段(包含主机名值) 我想从表2中选择表1中不存在的记录 select * from table2 where name NOT IN (select hostname from table1); 两个表中的na

我有一个mysql table1,其中包含
hostname
字段和以下值:

HostName
-----
sa77.com
ca77cded.com
sa65yacd.com
ca65zededs.com
sa88y.com
sa99ujk8.com
我还有另一个表2,它有
name
字段(包含主机名值)

我想从表2中选择表1中不存在的记录

select * 
from table2 
where name NOT IN (select hostname from table1);
两个表中的
name
hostname
中的服务器名称可能是完全限定的,也可能不是完全限定的。 例如,
sa77
的值可以是
sa77.abc.com
sa77.cde
sa77
。服务器可以有多个域值

sa77.abc.com
匹配
sa77
sa77.abc
我基本上需要比较
sa77
的值:

where name NOT IN (select hostname from table1)
  and CONCAT(name, '.com') NOT IN (select hostname from table1);

但是您需要进一步解释需要匹配哪些情况。

我通常使用
左连接和
where is null
来进行匹配,如下所示:

select * 
from table2
left join table1 on table2.name=table1.hostname
where table1.hostname is null;

(编辑,因为您希望表2中的记录不在表1中,而不是相反。)

问题是什么?您只是想知道如何使用联接来实现这一点吗?问题是在表1
hostname
中,值可以是
sa77.com
sa77
我想检查这两个值,这两个值表示服务器的
FQDN
?在给定的示例中,表1似乎总是有.com,而表2可能有,也可能没有。请澄清。我已经编辑了我的问题。两个表中的name或hostname中的服务器名可能是完全限定的,也可能不是完全限定的。事实上,这应该是最有效的方法,正如大家所同意的那样——它产生正确的结果,并且比其他示例中建议的使用CONCAT更有效。
select * 
from table2
left join table1 on table2.name=table1.hostname
where table1.hostname is null;