是否有一个 SQL 函数可以生成给定范围的序列号?
我需要生成一个具有给定范围的连续整数数组,以便将其用于:
SELECT tbl.pk_id
FROM tbl
WHERE tbl.pk_id NOT IN (sequential array);
I need to generate an array of sequential integers with a given range in order to use it in:
SELECT tbl.pk_id
FROM tbl
WHERE tbl.pk_id NOT IN (sequential array);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有给定的连续整数范围(即起点和终点),您应该能够使用
BETWEEN
关键字:还是我误解了您的问题..?
If you have a given range - ie a start point and an end point - of sequential integers you should just be able to use the
BETWEEN
keyword:or am I misunderstanding your question..?
因为你说你已经有了一个数字表,所以我建议这样做:
可能比你尝试过的查询更有效。
Because you say you've already got a number table, I would suggest this:
Might possibly be more efficient than the query you've tried.
两个想法。 。 。
首先,没有标准的 SQL 函数可以做到这一点。但有些系统包含确实生成序列的非标准函数。例如,在 PostgreSQL 中,您可以使用generate_series() 函数。
该函数本质上返回一个表;它可以在连接中使用。
如果 Informix 没有执行类似操作的函数,也许您可以编写一个执行类似操作的 Informix SPL 函数。
其次,您可以创建一个单列表并用一系列整数填充它。这适用于所有平台,并且不需要编程。它只需要最少的维护。 (您需要在此表中保留比在生产表中使用的整数更多的整数。)
使用电子表格或实用程序生成一系列整数来填充它。如果您有 Unix、Linux 或 Cygwin 环境,最简单的方法是使用
seq
。Informix 有一个免费开发者版本供您下载。也许你可以用它构建一个引人注目的演示,并且管理层会让你升级。
Two thoughts . . .
First, there's no standard SQL function that does that. But some systems include a non-standard function that does generates a series. In PostgreSQL, for example, you can use the generate_series() function.
That function essentially returns a table; it can be used in joins.
If Informix doesn't have a function that does something similar, maybe you can write an Informix SPL function that does.
Second, you could just create a one-column table and populate it with a series of integers. This works on all platforms, and doesn't require programming. It requires only minimal maintenance. (You need to keep more integers in this table than you're using in your production table.)
Use a spreadsheet or a utility program to generate a series of integers to populate it. The easiest way if you have a Unix, Linux, or Cygwin environment laying around is to use
seq
.Informix has a free developer version you can download. Maybe you can build a compelling demo with it, and management will let you upgrade.
我将建议一个通用的解决方案来创建一个包含给定 k 的正整数 0 .. 2^k-1 的结果集,以便随后用作子查询、视图或物化视图。
下面的代码说明了 k=2 的技术。
我希望这对您的任务有所帮助,
最诚挚的问候,
卡斯滕
i'll suggest a generic solution to create a result set containing the positive integers 0 .. 2^k-1 for a given k for subsequent use as a subquery, view or materialized view.
the code below illustrates the technique for k=2.
i hope that helps you with your task
best regards,
carsten