首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL查询选择公共IP地址

SQL查询选择公共IP地址
EN

Stack Overflow用户
提问于 2013-03-28 23:38:25
回答 2查看 3.3K关注 0票数 2

我在PostgreSQL8.3数据库上。我正试图在下面的查询中找出错误所在。我试图设计一个查询,只选择属于私有地址的source_ips和destination_ips。

出于某种原因,在下面的查询中捕获的地址之一是地址208.117.252.39,它不是私有地址。

下面的查询中的逻辑是否有问题,使得它也会选择公共IP地址?

代码语言:javascript
复制
 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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-29 00:16:59

您需要适当地分组您的最后条件。现在,最后一个“或”忽略了所有的source_ip条件。

将查询构造为这样:

代码语言:javascript
复制
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
);

注意目标子句是分组在一起的。

票数 1
EN

Stack Overflow用户

发布于 2013-03-29 00:17:28

由于and操作比or具有优先级,所以缺少一个括号。您的查询相当于:

代码语言:javascript
复制
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

正确版本:

代码语言:javascript
复制
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
    )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15694047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档