我使用的是 postgresql 8.3 数据库。我试图找出下面的查询中的错误在哪里。我正在尝试设计一个查询来仅选择私有(private)地址的 source_ips 和 destination_ips。
由于某种原因,下面的查询中获取的地址之一是地址 208.117.252.39,它不是私有(private)地址。
Is there something wrong with the logic in the query below that would make it select public IP addresses as well?
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
最佳答案
由于 and
运算的优先级高于 or
,因此您缺少括号。您的查询相当于:
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
正确的版本:
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
)
关于postgresql - SQL查询选择公共(public)IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15694047/