Skip to content
Advertisement

How to return boolean value if sub-query returns any result

I am attempting to write a query to audit a table of IP addresses against a table of overlapping net blocks. Individual net blocks can be marked active or inactive via a BOOLEAN. The audit query should return a BOOLEAN as active depending upon the active column in the netblock table. If any net block has the active column set to FALSE, then the IP addresses contained within the netblock should report as active set to FALSE.

The following is the definition of the ip table:

The following are example entries in ip:

The following is the definition of the netblock table:

The following are example entries in the netblock table:

I have figure out a query to list all “active” and “inactive” IP addresses in separate queries:

Results of above query:

How do I replace the static value of unknown for the active column in the following table by queuing the netblock table for inactvie/active net blocks?

Results from above query:

Advertisement

Answer

You can use ALL and a subquery to check if all the net blocks the IP is in are active.

Alternatively you can use NOT EXISTS:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement