我想根据表上的主机名字段使用 SQL 加入 DB2 中的两个表,但必须使用一些字符串函数,
I Want to Join two tables in DB2 using SQL based on Hostname Field on tables, but have to use some String Functions,
我想根据表上的主机名字段连接 DB2 中的两个表,但问题是主机名字段包含 IP 地址以及服务器的主机名。但是在考虑主机名时,我必须在第一个 '.' 出现之前选择主机名。在 IP 地址的情况下,我必须与整个字符串匹配。
Table一个
HostName | TableA_ID
abc.com 1
cdf.com 2
12.23.3.1 3
12.23.3.2 4
12.23.3.3 5
Table B
HostName | TableB_ID
abc.in 6
cdf.in 7
12.23.3.1 8
12.23.3.2 9
加入Table
HostName | TableA_ID | TableB_ID
abc 1 6
cdf 2 7
12.23.3.1 3 8
12.23.3.2 4 9
我已经尝试实现 REGEX,但似乎不起作用,请帮帮我
这似乎有效
with
a (hostname, TableA_ID ) as (
values
('abc.com', 1),
('cdf.com', 2),
('12.23.3.1', 3),
('12.23.3.2', 4),
('12.23.3.3', 5)
),
b (hostname, TableB_ID ) as (
values
('abc.in', 6),
('cdf.in', 7),
('12.23.3.1', 8),
('12.23.3.2', 9)
)
select
a.hostname, TableA_ID, TableB_ID
from a
inner join b on a.hostname = b.hostname
where regexp_like(a.hostname,'\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z')
union all
select
a.hostname, TableA_ID, TableB_ID
from a
inner join b on regexp_substr(a.hostname, '\A([^.]*)')
= regexp_substr(b.hostname,'\A([^.]*)')
where NOT regexp_like(a.hostname,'\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z')
order by hostname
我想根据表上的主机名字段连接 DB2 中的两个表,但问题是主机名字段包含 IP 地址以及服务器的主机名。但是在考虑主机名时,我必须在第一个 '.' 出现之前选择主机名。在 IP 地址的情况下,我必须与整个字符串匹配。
Table一个
HostName | TableA_ID
abc.com 1
cdf.com 2
12.23.3.1 3
12.23.3.2 4
12.23.3.3 5
Table B
HostName | TableB_ID
abc.in 6
cdf.in 7
12.23.3.1 8
12.23.3.2 9
加入Table
HostName | TableA_ID | TableB_ID
abc 1 6
cdf 2 7
12.23.3.1 3 8
12.23.3.2 4 9
我已经尝试实现 REGEX,但似乎不起作用,请帮帮我
这似乎有效
with
a (hostname, TableA_ID ) as (
values
('abc.com', 1),
('cdf.com', 2),
('12.23.3.1', 3),
('12.23.3.2', 4),
('12.23.3.3', 5)
),
b (hostname, TableB_ID ) as (
values
('abc.in', 6),
('cdf.in', 7),
('12.23.3.1', 8),
('12.23.3.2', 9)
)
select
a.hostname, TableA_ID, TableB_ID
from a
inner join b on a.hostname = b.hostname
where regexp_like(a.hostname,'\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z')
union all
select
a.hostname, TableA_ID, TableB_ID
from a
inner join b on regexp_substr(a.hostname, '\A([^.]*)')
= regexp_substr(b.hostname,'\A([^.]*)')
where NOT regexp_like(a.hostname,'\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z')
order by hostname