我在PostgreSQL8.3数据库上。我正试图在下面的查询中找出错误所在。我试图设计一个查询,只选择属于私有地址的source_ips和destination_ips。
出于某种原因,在下面的查询中捕获的地址之一是地址208.117.252.39,它不是私有地址。
下面的查询中的逻辑是否有问题,使得它也会选择公共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发布于 2013-03-29 00:16:59
您需要适当地分组您的最后条件。现在,最后一个“或”忽略了所有的source_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
);注意目标子句是分组在一起的。
发布于 2013-03-29 00:17:28
由于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
)https://stackoverflow.com/questions/15694047
复制相似问题