SQL生成从1到100的数字列表
使用 DUAL 表,如何获得从 1 到 100 的数字列表?
Using the DUAL table, how can I get a list of numbers from 1 to 100?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
使用 DUAL 表,如何获得从 1 到 100 的数字列表?
Using the DUAL table, how can I get a list of numbers from 1 to 100?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(14)
Peter 示例的一个变体,演示了一种可用于生成 0 到 99 之间的所有数字的方法。
当您进行批量标识符分配并查找尚未分配的项目时,类似的方法会变得很有用。
例如,如果您销售宾果游戏门票,您可能需要分批分配 100 名场内工作人员(猜猜我以前是如何为体育赛事筹集资金的)。当他们销售一批产品时,他们会按顺序获得下一批产品。但是,购买门票的人可以选择购买该批次中的任何门票。可能会问“已经售出哪些票”的问题。
在这种情况下,我们只有给定批次内返回的部分随机票证列表,并且需要所有可能性的完整列表来确定我们没有哪些票证。
请原谅使用关键字,我更改了现实世界示例中的一些变量名称。
...为了证明为什么这样的东西会有用
A variant of Peter's example, that demonstrates a way this could be used to generate all numbers between 0 and 99.
Something like this becomes useful when you are doing batch identifier assignment, and looking for the items that have not yet been assigned.
For example, if you are selling bingo tickets, you may want to assign batches of 100 floor staff (guess how i used to fund raise for sports). As they sell a batch, they are given the next batch in sequence. However, people purchasing the tickets can select to purchase any tickets from the batch. The question may be asked, "what tickets have been sold".
In this case, we only have a partial, random, list of tickets that were returned within the given batch, and require a complete list of all possibilities to determine which we don't have.
Excuse the use of key words, I changed some variable names from a real world example.
... To demonstrate why something like this would be useful
我创建了一个返回数字表的 Oracle 函数
有必要创建一个新的数据类型:
用法:
如果您需要通过指数表示法在数字之间进行小数:
I created an Oracle function that returns a table of numbers
Is necessary create a new data type:
Usage:
And if you need decimals between numbers by exponencial notation:
如果要生成数字 1 - 100 的列表,可以使用 {1,2,3,4,5,6,6,7,8,9,10} X {0,10,20, 30,40,50,60,70,80,90}
https://en.wikipedia.org/wiki/Cartesian_product
大致如下:
我无法在 Oracle 数据库上测试这一点,您可以将双精度放在它所属的位置,但它应该可以工作。
If you want to generate the list of numbers 1 - 100 you can use the cartesian product of {1,2,3,4,5,6,6,7,8,9,10} X {0,10,20,30,40,50,60,70,80,90}
https://en.wikipedia.org/wiki/Cartesian_product
Something along the lines of the following:
I'm not able to test this out on an oracle database, you can place the dual where it belongs but it should work.
最简单的解决方案是使用公共表表达式递归逻辑,如下所示:
The simplest solution would be to use common table expression recursion logic as in below:
上面的查询是在数据库中用SQL编写的。
The above query is written in SQL in the database.
你的问题很难理解,但如果你想选择从
1
到100
的数字,那么这应该可以解决问题:Your question is difficult to understand, but if you want to select the numbers from
1
to100
, then this should do the trick:ORACLE PL/SQL 中另一个有趣的解决方案:
Another interesting solution in ORACLE PL/SQL:
使用Oracle的子查询工厂子句:“WITH”,您可以选择从1到100的数字:
Using Oracle's sub query factory clause: "WITH", you can select numbers from 1 to 100:
您可以使用
XMLTABLE
:DBFiddle 演示
You could use
XMLTABLE
:DBFiddle Demo
用困难的方式去做吧。使用很棒的
MODEL
子句:证明:http://sqlfiddle.com /#!4/d41d8/20837
Do it the hard way. Use the awesome
MODEL
clause:Proof: http://sqlfiddle.com/#!4/d41d8/20837
如果您希望整数绑定在两个整数之间(即以 1 以外的数字开头),您可以使用如下所示的内容:
它给出:
If you want your integers to be bound between two integers (i.e. start with something other than 1), you can use something like this:
It gives:
彼得的回答也是我最喜欢的。
如果您正在寻找更多详细信息,IMO,此处有一个非常好的概述。
特别有趣的是阅读基准。
Peter's answer is my favourite, too.
If you are looking for more details there is a quite good overview, IMO, here.
Especially interesting is to read the benchmarks.
使用
GROUP BY CUBE
:Rextester 演示
Using
GROUP BY CUBE
:Rextester Demo