Postgres CIDR。如何仅选择没有子网的网络?

发布于 2024-12-15 13:39:25 字数 525 浏览 4 评论 0原文

我有一个表 Net,其中是网络列表

-- 表:net

CREATE TABLE net
(
  id serial NOT NULL,
  cidr cidr,
  description text,
  CONSTRAINT net_pkey PRIMARY KEY (id )
)

我需要选择所有不存在的网络。属于其他网络。即只有没有子网的网络。如何构建查询?

西德:
10.0.0.0/8
10.1.0.0/16
10.2.0.0/16
10.3.0.0/16
10.3.1.0/24
10.3.2.0/24
10.3.3.0/24
10.15.1.0/24
10.15.2.0/24
10.15.3.0/24
172.20.0.0/16
172.21.0.0/16
172.0.0.0/8
11.11.11.0/24
评分最高的网络为 10.0.0.0/8、172.0.0.0/8、11.11.11.0/24

I have a table Net where is the list of networks

-- Table: net

CREATE TABLE net
(
  id serial NOT NULL,
  cidr cidr,
  description text,
  CONSTRAINT net_pkey PRIMARY KEY (id )
)

I need to select all the networks that do not. belong to other networks. ie only network without subnets. How to build a query?

cidr:
10.0.0.0/8
10.1.0.0/16
10.2.0.0/16
10.3.0.0/16
10.3.1.0/24
10.3.2.0/24
10.3.3.0/24
10.15.1.0/24
10.15.2.0/24
10.15.3.0/24
172.20.0.0/16
172.21.0.0/16
172.0.0.0/8
11.11.11.0/24
Top rated net is 10.0.0.0/8, 172.0.0.0/8, 11.11.11.0/24

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Saygoodbye 2024-12-22 13:39:25

我是这样理解你的问题的:
您需要不包含在任何其他网络规范中的所有网络规范,即不属于另一个网络规范的一部分。

试试这个:

SELECT *
FROM   net
WHERE  NOT EXISTS (SELECT cidr FROM net n WHERE n.cidr >> net.cidr);

产生您预期的结果。
查看网络地址函数和运算符一章有关 >> 运算符的更多信息,请参阅手册。
半连接 NOT EXISTS 可能是最快的方法。

I understand your question like this:
You want all network specifications that are not contained in any other network specification, i.e. are not part of the subnet of another.

Try this:

SELECT *
FROM   net
WHERE  NOT EXISTS (SELECT cidr FROM net n WHERE n.cidr >> net.cidr);

Produces your expected result.
Have a look at the chapter Network Address Functions and Operators in the manual for more on the >> operator.
Semi-join with NOT EXISTS is probably the fastest way to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文