|
Home > Archive > PostgreSQL SQL > April 2005 > searching cidr/inet arrays
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
searching cidr/inet arrays
|
|
| Rob Casson 2005-04-25, 8:25 pm |
| i'm having trouble figuring out how to search in inet arrays....its
been a long time since i used postgres array support, so i may just be
bone-headed......
how can i determine if a given ip address is contained in the subnet
declaration inside an array?
{134.53.25.0/24,134.53.0.0/16}
{134.53.24.0/24}
i'd like to see which rows match an ip of, say, 134.53.24.2.....
thanks in advance, and sorry if this is a faq....i've googled
site:archives.postgresql.org, but haven't found my solution.....
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql
.org so that your
message can get through to the mailing list cleanly
| |
| Michael Fuhr 2005-04-25, 8:25 pm |
| On Mon, Apr 25, 2005 at 02:46:37PM -0400, Rob Casson wrote:
>
> i'm having trouble figuring out how to search in inet arrays....its
> been a long time since i used postgres array support, so i may just be
> bone-headed......
>
> how can i determine if a given ip address is contained in the subnet
> declaration inside an array?
>
> {134.53.25.0/24,134.53.0.0/16}
> {134.53.24.0/24}
>
> i'd like to see which rows match an ip of, say, 134.53.24.2.....
See "Row and Array Comparisons" in the "Functions and Operators"
chapter of the documentation. The following works in 7.4 and later:
CREATE TABLE foo (
id serial PRIMARY KEY,
nets cidr[] NOT NULL
);
INSERT INTO foo (nets) VALUES ('{134.53.25.0/24,134.53.0.0/16}');
INSERT INTO foo (nets) VALUES ('{134.53.24.0/24}');
SELECT * FROM foo WHERE '134.53.24.2' << ANY (nets);
id | nets
----+--------------------------------
1 | {134.53.25.0/24,134.53.0.0/16}
2 | {134.53.24.0/24}
(2 rows)
SELECT * FROM foo WHERE '134.53.100.2' << ANY (nets);
id | nets
----+--------------------------------
1 | {134.53.25.0/24,134.53.0.0/16}
(1 row)
--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster
|
|
|
|
|