postgresql - SQL查询选择公共(public)IP地址

标签 postgresql ip postgresql-8.3

我使用的是 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/

相关文章:

ruby-on-rails - 在 Heroku 上部署失败并出现 PostgreSQL ActionView::Template::Error

postgresql - 集群 PostgreSQL 集群

Powershell 将字符串转换为 System.Net.IPAddress

我可以在 C 中使用 int 类型作为 IP 地址的网络掩码吗?

postgresql - 在非常大的表上运行 AUTO_VACUUM(以防止环绕)时出现问题

postgresql - 如何从 pl/pgsql 函数中调用 shell 命令?

postgresql - 记录来自 Sequelize 的 postgres 输出消息

string - 如何遍历 Postgresql 中字符串的字符?

IP 客户端 WSO2 Esb

sql - 这两个 PostgreSQL 函数是否等价? (返回表和返回集)