根据主机名查找匹配的列值

s2j5cfk0  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(414)

我有一张table table1b_id 作为 PK , ip , host 在此表中,不同的主机可以具有相同的IP地址, t2_id 是的外键 idtable2 ```
b_id ip host t2_id
9205 10.10.10.10 zero 1121
9206 10.10.10.10 hello.abc.com 1121
9207 10.10.10.10 hi.abc.com 1121

我还有一张table
table2 `id` 作为 `PK` ,  `ip` ,  `host` 以及 `b_id` 在这里 `b_id` 是的外键 `table1` 在我的项目中,两个主机名 `hi..com` 以及 `hi` 因为域名 `.com` 不考虑比较。所以,我只需要比较一下 `hostname without domain name` ```
id      ip              host             b_id
1121    10.10.10.10     hi               null

我想从中筛选出行 table1 它们有相同的 ip 价值观,但 host 所以,我的输出应该是

b_id    ip              host             t2_id
9205    10.10.10.10     zero             1121
9206    10.10.10.10     hello.abc.com    1121

我写了下面的查询,但它没有给出正确的结果

select * from table1 T1 INNER JOIN table2 T2
ON T1.t2_id = T2.id
where T1.host not like T2.host;
igsr9ssn

igsr9ssn1#

用这个怎么样 not exists ?

select t1.*
from table1 t1
where not exists (select 1
                  from table2 t2
                  where t2.ip = t1.ip and
                        substring_index(t2.host, '.', 1) = substring_index(t1.host, '.', 1)
                 ) ;

相关问题