PostgreSQL, CIDR - search all ip addresses that are cotained within networks - from cidr array

StackOverflow https://stackoverflow.com/questions/23077853

Pregunta

I have a table that contains two cidr arrays. One contains hosts ip addresses and the second one network addresses.

I need to write a function that would execute query like this:

SELECT * from sometable 
WHERE ip_addr IN( 'all items from first array') OR
ip_addr << 'all item from second array'

So basically I need to search records which ip_addr equals to one from the first array or ip_addr is contained within one of the networks from the second array.

¿Fue útil?

Solución

If I understand you correctly, you're looking for the ANY comparison function

SELECT * 
  FROM sometable 
 WHERE (   ip_addr  = ANY ('all items from first array') 
        OR ip_addr << ANY ('all item from second array')
       )
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top