查找分配给单个路由器但也属于公共路由器的IP的IP

时间:2020-09-22 19:50:48

标签: sql oracle oracle12c

请参阅我的表格(RouterIP),其中包含3列及以下数据: 我想找到分配给特定路由器的IP,它们也是普通路由器的一部分。通用路由器也可能具有范围。 因此,我需要检查这些范围之间的路由器IP并填充数据。

请提出解决此问题的任何建议。

RouterIP

所需的输出:

IPTAKEN

以文本格式链接到数据:

https://pastebin.ubuntu.com/p/YGRGQJJQ7n/

2 个答案:

答案 0 :(得分:1)

Oracle没有比较友好的方式来比较IP地址,因此每个数字都需要单独比较。您可以尝试以下解决方案,看看它是否适合您。我添加了名为COMMON_ROUTER_FROM_IP的第三列,以显示IP地址在该范围内的CommonRouter的FROM_IP。如果不需要,可以将其删除。

查询

WITH
    routerip (TYPE, from_ip, to_ip)
    AS
        (SELECT 'Router1', '192.168.0.1', '192.168.0.1' FROM DUAL
         UNION ALL
         SELECT 'CommonRouter', '192.167.0.0', '192.167.0.90' FROM DUAL
         UNION ALL
         SELECT 'Router2', '10.120.0.1', '10.120.0.1' FROM DUAL
         UNION ALL
         SELECT 'CommonRouter', '10.120.0.1', '10.120.0.99' FROM DUAL
         UNION ALL
         SELECT 'Router3', '10.120.0.45', '10.120.0.45' FROM DUAL
         UNION ALL
         SELECT 'Router4', '10.120.0.34', '10.120.0.34' FROM DUAL
         UNION ALL
         SELECT 'Router5', '192.167.0.49', '192.167.0.49' FROM DUAL
         UNION ALL
         SELECT 'Router6', '192.128.0.1', '192.128.0.1' FROM DUAL
         UNION ALL
         SELECT 'Router6', '192.167.0.9', '192.167.0.9' FROM DUAL
         UNION ALL
         SELECT 'Router3', '10.120.0.3', '10.120.0.3' FROM DUAL)
SELECT r1.TYPE,
       r1.from_ip,
       (SELECT from_ip
                  FROM routerip r2
                 WHERE r2.TYPE = 'CommonRouter'
                   and to_number(REGEXP_SUBSTR (r1.from_ip, '[^.]+',1,1)) between to_number(REGEXP_SUBSTR (r2.from_ip, '[^.]+',1,1)) and to_number(REGEXP_SUBSTR (r2.to_ip, '[^.]+',1,1))
                   and to_number(REGEXP_SUBSTR (r1.from_ip, '[^.]+',1,2)) between to_number(REGEXP_SUBSTR (r2.from_ip, '[^.]+',1,2)) and to_number(REGEXP_SUBSTR (r2.to_ip, '[^.]+',1,2))
                   and to_number(REGEXP_SUBSTR (r1.from_ip, '[^.]+',1,3)) between to_number(REGEXP_SUBSTR (r2.from_ip, '[^.]+',1,3)) and to_number(REGEXP_SUBSTR (r2.to_ip, '[^.]+',1,3))
                   and to_number(REGEXP_SUBSTR (r1.from_ip, '[^.]+',1,4)) between to_number(REGEXP_SUBSTR (r2.from_ip, '[^.]+',1,4)) and to_number(REGEXP_SUBSTR (r2.to_ip, '[^.]+',1,4))
                   and rownum = 1) as common_router_from_ip
  FROM routerip r1
 WHERE     r1.TYPE <> 'CommonRouter'
       AND EXISTS
               (SELECT 1
                  FROM routerip r2
                 WHERE r2.TYPE = 'CommonRouter'
                   and to_number(REGEXP_SUBSTR (r1.from_ip, '[^.]+',1,1)) between to_number(REGEXP_SUBSTR (r2.from_ip, '[^.]+',1,1)) and to_number(REGEXP_SUBSTR (r2.to_ip, '[^.]+',1,1))
                   and to_number(REGEXP_SUBSTR (r1.from_ip, '[^.]+',1,2)) between to_number(REGEXP_SUBSTR (r2.from_ip, '[^.]+',1,2)) and to_number(REGEXP_SUBSTR (r2.to_ip, '[^.]+',1,2))
                   and to_number(REGEXP_SUBSTR (r1.from_ip, '[^.]+',1,3)) between to_number(REGEXP_SUBSTR (r2.from_ip, '[^.]+',1,3)) and to_number(REGEXP_SUBSTR (r2.to_ip, '[^.]+',1,3))
                   and to_number(REGEXP_SUBSTR (r1.from_ip, '[^.]+',1,4)) between to_number(REGEXP_SUBSTR (r2.from_ip, '[^.]+',1,4)) and to_number(REGEXP_SUBSTR (r2.to_ip, '[^.]+',1,4)))
 ORDER BY r1.type;

结果

      TYPE         FROM_IP    COMMON_ROUTER_FROM_IP
__________ _______________ ________________________
Router2    10.120.0.1      10.120.0.1
Router3    10.120.0.3      10.120.0.1
Router3    10.120.0.45     10.120.0.1
Router4    10.120.0.34     10.120.0.1
Router5    192.167.0.49    192.167.0.0
Router6    192.167.0.9     192.167.0.0

答案 1 :(得分:0)

这是我的理解方式:

select a.type, a.from_ip
from router a join router b on a.from_ip between b.from_ip and b.to_ip 
where b.type =  'CommonRouter'
  and a.type <> 'CommonRouter'