2016-11-18 4 views
4

PostgreSQL에서는 아래와 같이 CIDR 범위의 상한 및 하한 경계를 얻을 수 있습니다.

그러나 두 IP 주소 (SQL에 의해)에서 CIDR을 가져올 수 있습니까?
예 :PostgreSQL의 두 IP에서 CIDR을 얻으려면 어떻게해야합니까?

입력 "192.168.0.0", "192.168.255.255"
출력은 "192.168.0.0/16는"

SELECT 
    network 
    ,network::cidr 
    -- http://technobytz.com/ip-address-data-types-postgresql.html 
    --,netmask(network::cidr) AS nm 
    --,~netmask(network::cidr) AS nnm 
    ,host(network::cidr) AS lower 
    ,host(broadcast(network::cidr)) AS upper -- broadcast: last address in the range 
    ,family(network::cidr) as fam -- IPv4, IPv6 
    ,masklen(network::cidr) as masklen 
FROM 
(
      SELECT CAST('192.168.1.1/32' AS varchar(100)) as network 
    UNION SELECT CAST('192.168.0.0/16' AS varchar(100)) as network 
    --UNION SELECT CAST('192.168.0.1/16' AS varchar(100)) as network 

) AS tempT 
+2

['select inet_merge ('192.168.0.0', '192.168.255.255');'] (https://www.postgresql.org/docs/current/static/functions-net.html) – Abelisto

답변

5

나는 당신이 inet_merge 찾고있는 생각 :

test=> SELECT inet_merge('192.168.0.0', '192.168.128.255'); 
┌────────────────┐ 
│ inet_merge │ 
├────────────────┤ 
│ 192.168.0.0/16 │ 
└────────────────┘ 
(1 row)