Re: CIdr query qestion

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Michele Petrazzo - Unipex srl <michele(dot)petrazzo(at)unipex(dot)it>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: CIdr query qestion
Date: 2007-07-10 09:16:43
Message-ID: 20070710091643.GA48320@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, Jul 10, 2007 at 09:36:49AM +0200, Michele Petrazzo - Unipex srl wrote:
> A question about cidr query. I'm tring to use the various cidr functions
> to exclude a A sub class, but I don't understand how use it:
>
> SELECT count(ip_addr), ip_addr from wi_log_network where ip_addr !=
> network('10.10.0.0/16', 16) group by (ip_addr);
>
> it don't exclude the 10.10 ! Tried also the abbr or set_masklen keyword,
> but the same. How do it?

Please post the actual query you're running. The above fails with

ERROR: function network("unknown", integer) does not exist

Does the following help?

CREATE TABLE test (id integer PRIMARY KEY, ip_addr inet NOT NULL);
INSERT INTO test (id, ip_addr) VALUES (1, '10.2.3.4');
INSERT INTO test (id, ip_addr) VALUES (2, '10.10.5.6');

SELECT id,
ip_addr,
set_masklen(ip_addr, 16),
network(set_masklen(ip_addr, 16)),
network(set_masklen(ip_addr, 16)) = '10.10.0.0/16',
'10.10.0.0/16'::cidr >> ip_addr,
NOT '10.10.0.0/16'::cidr >> ip_addr
FROM test;

id | ip_addr | set_masklen | network | ?column? | ?column? | ?column?
----+-----------+--------------+--------------+----------+----------+----------
1 | 10.2.3.4 | 10.2.3.4/16 | 10.2.0.0/16 | f | f | t
2 | 10.10.5.6 | 10.10.5.6/16 | 10.10.0.0/16 | t | t | f
(2 rows)

> P.s. Tried the >>, but it only include the values that I want to
> exclude! (And I don't found how to do the opposite!)

You can use NOT to negate a boolean expression; see above.

--
Michael Fuhr

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Eisentraut 2007-07-10 14:24:47 Database system identifier from SQL
Previous Message Jesper K. Pedersen 2007-07-10 09:00:12 Re: CIdr query qestion