Postgresql 选择公共IP地址的SQL查询

Postgresql 选择公共IP地址的SQL查询,postgresql,ip,postgresql-8.3,Postgresql,Ip,Postgresql 8.3,我使用的是postgresql 8.3数据库。我正试图找出下面查询中的错误所在。我正在尝试设计一个查询,以仅选择作为专用地址的源IP和目标IP 出于某种原因,下面查询中抓取的地址之一是地址208.117.252.39,它不是专用地址 下面查询中的逻辑是否有问题,导致它也选择公共IP地址 选择源ip、目标ip 从ip_表 哪里 ( inet'10/8'>>源ip 或inet'192.168/16'>>源ip 或源ip>=inet'172.16/16'和源ip>目的地ip 或inet'192.168

我使用的是postgresql 8.3数据库。我正试图找出下面查询中的错误所在。我正在尝试设计一个查询,以仅选择作为专用地址的源IP和目标IP

出于某种原因,下面查询中抓取的地址之一是地址208.117.252.39,它不是专用地址

下面查询中的逻辑是否有问题,导致它也选择公共IP地址

选择源ip、目标ip
从ip_表
哪里
(
inet'10/8'>>源ip
或inet'192.168/16'>>源ip
或源ip>=inet'172.16/16'和源ip>目的地ip
或inet'192.168/16'>>目的地ip

您需要对最终条件进行适当分组。现在,最后一个“或”忽略所有
源ip
条件

按如下方式构造查询:

select source_ip, destination_ip
from ip_table
where
(
  inet '10/8' >> source_ip
    or inet '192.168/16' >> source_ip
    or inet '172.16/12' >> source_ip
 )
and  (
  inet '10/8' >> destination_ip
    or  inet '192.168/16' >> destination_ip
    or inet '172.16/12' >> destination_ip
);

注意,destination子句被分组在一起。

由于
操作的优先级高于
,因此缺少括号。您的查询相当于:

select source_ip, destination_ip
from ip_table
where
    (
        (
        inet '10/8' >> source_ip
        or inet '192.168/16' >> source_ip
        or source_ip >= inet '172.16/16' and source_ip < inet '172.32/16'
        )
        and  inet '10/8' >> destination_ip
    )
    or  inet '192.168/16' >> destination_ip
选择源ip、目标ip
从ip_表
哪里
(
(
inet'10/8'>>源ip
或inet'192.168/16'>>源ip
或源ip>=inet'172.16/16'和源ip>目的地ip
)
或inet'192.168/16'>>目的地ip
正确版本:

select source_ip, destination_ip
from ip_table
where
    (
        inet '10/8' >> source_ip
        or inet '192.168/16' >> source_ip
        or source_ip >= inet '172.16/16' and source_ip < inet '172.32/16'
    )
    and
    (
        inet '10/8' >> destination_ip
        or  inet '192.168/16' >> destination_ip
    )
选择源ip、目标ip
从ip_表
哪里
(
inet'10/8'>>源ip
或inet'192.168/16'>>源ip
或源ip>=inet'172.16/16'和源ip>目的地ip
或inet'192.168/16'>>目的地ip
)

如果将>=更改为>>和<更改为>>是否相同?为了清楚起见,请尝试将“source\u ip>=inet'172.16/16”和source\u ip>source\u ip”。该专用范围表示为a/12 CIDR,不需要专门处理。
select source_ip, destination_ip
from ip_table
where
    (
        inet '10/8' >> source_ip
        or inet '192.168/16' >> source_ip
        or source_ip >= inet '172.16/16' and source_ip < inet '172.32/16'
    )
    and
    (
        inet '10/8' >> destination_ip
        or  inet '192.168/16' >> destination_ip
    )