2

私はpostgresql 8.3データベースを使用しています。以下のクエリで間違いがどこにあるかを把握しようとしています。プライベート アドレスである source_ips と destination_ips のみを選択するクエリを設計しようとしています。

何らかの理由で、以下のクエリで取得されたアドレスの 1 つは、プライベート アドレスではないアドレス 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
4

2 に答える 2

2

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
    )
于 2013-03-29T00:17:28.423 に答える
1

最終条件を適切にグループ化する必要があります。現在、最後の「または」はすべての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-29T00:16:59.940 に答える