SQL来生成一个从1到100的数字列表

使用DUAL表,我怎样才能得到从1到100的数字列表?

你的问题很难理解,但是如果你想select从1100的数字,那么这应该做的伎俩:

 Select Rownum r From dual Connect By Rownum <= 100 

ORACLE PL / SQL中另一个有趣的解决scheme:

  SELECT LEVEL n FROM DUAL CONNECT BY LEVEL <= 100; 

这样做很难。 使用真棒MODEL子句:

 SELECT V FROM ( SELECT 1 V FROM DUAL ) T MODEL DIMENSION BY (ROWNUM R) MEASURES (V) RULES ITERATE (100) ( V[ITERATION_NUMBER] = CV(R) + 1 ) ORDER BY 1 

certificate: http : //sqlfiddle.com/#!4/ d41d8/ 20837

彼得的回答也是我最喜欢的。

如果你正在寻找更多的细节, 这里有一个很好的概述,国际海事组织。
特别有趣的是阅读基准 。

如果你想让你的整数在两个整数之间进行绑定(即从1开始),你可以使用这样的东西:

 with bnd as (select 4 lo, 9 hi from dual) select (select lo from bnd) - 1 + level r from dual connect by level <= (select hi-lo from bnd); 

它给:

 4 5 6 7 8 

使用GROUP BY CUBE

 SELECT ROWNUM FROM (SELECT 1 AS c FROM dual GROUP BY CUBE(1,1,1,1,1,1,1) ) sub WHERE ROWNUM <=100; 

Rextester演示

彼得的例子的一个变种,演示了这种方式可以用来产生0到99之间的所有数字。

 with digits as ( select mod(rownum,10) as num from dual connect by rownum <= 10 ) select a.num*10+b.num as num from digits a ,digits b order by num ; 

如果您正在进行批次标识符分配,并且查找尚未分配的项目,则这样的事情会变得很有用。

例如,如果您正在销售宾果游戏门票,您可能需要分配100个楼层的工作人员(猜猜我是如何筹集体育费用的)。 当他们出售一批时,他们依次被给予下一批。 但是,购买票的人可以select从该批购买任何票。 这个问题可能会被问到:“什么票已经售出”。

在这种情况下,我们只有在给定的批次中返回的部分,随机的票据列表,并且需要所有可能性的完整列表来确定我们没有的列表。

 with range as ( select mod(rownum,100) as num from dual connect by rownum <= 100 ), AllPossible as ( select a.num*100+b.num as TicketNum from batches a ,range b order by num ) select TicketNum as TicketsSold from AllPossible where AllPossible.Ticket not in (select TicketNum from TicketsReturned) ; 

请原谅使用关键词,我从一个真实世界的例子中改变了一些variables名。

…为了说明为什么这样的事情会有用

我创build了一个返回一个数字表的Oracle函数

 CREATE OR REPLACE FUNCTION [schema].FN_TABLE_NUMBERS( NUMINI INTEGER, NUMFIN INTEGER, EXPONENCIAL INTEGER DEFAULT 0 ) RETURN TBL_NUMBERS IS NUMEROS TBL_NUMBERS; INDICE NUMBER; BEGIN NUMEROS := TBL_NUMBERS(); FOR I IN ( WITH TABLA AS (SELECT NUMINI, NUMFIN FROM DUAL) SELECT NUMINI NUM FROM TABLA UNION ALL SELECT (SELECT NUMINI FROM TABLA) + (LEVEL*TO_NUMBER('1E'||TO_CHAR(EXPONENCIAL))) NUM FROM DUAL CONNECT BY (LEVEL*TO_NUMBER('1E'||TO_CHAR(EXPONENCIAL))) <= (SELECT NUMFIN-NUMINI FROM TABLA) ) LOOP NUMEROS.EXTEND; INDICE := NUMEROS.COUNT; NUMEROS(INDICE):= i.NUM; END LOOP; RETURN NUMEROS; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN NUMEROS; WHEN OTHERS THEN RETURN NUMEROS; END; / 

有必要创build一个新的数据types:

 CREATE OR REPLACE TYPE [schema]."TBL_NUMBERS" IS TABLE OF NUMBER; / 

用法:

 SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10))--integers difference: 1;2;.......;10 

如果你需要用数字符号表示小数点之间的数字:

 SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10,-1));--with 0.1 difference: 1;1.1;1.2;.......;10 SELECT COLUMN_VALUE NUM FROM TABLE([schema].FN_TABLE_NUMBERS(1,10,-2));--with 0.01 difference: 1;1.01;1.02;.......;10 

没有必要得到一个可怜的100个数字的所有聪明,只是蛮力;)

 select 1 from dual union select 2 from dual union select 3 from dual union select 4 from dual union select 5 from dual union select 6 from dual union select 7 from dual union select 8 from dual union select 9 from dual union select 10 from dual union select 11 from dual union select 12 from dual union select 13 from dual union select 14 from dual union select 15 from dual union select 16 from dual union select 17 from dual union select 18 from dual union select 19 from dual union select 20 from dual union select 21 from dual union select 22 from dual union select 23 from dual union select 24 from dual union select 25 from dual union select 26 from dual union select 27 from dual union select 28 from dual union select 29 from dual union select 30 from dual union select 31 from dual union select 32 from dual union select 33 from dual union select 34 from dual union select 35 from dual union select 36 from dual union select 37 from dual union select 38 from dual union select 39 from dual union select 40 from dual union select 41 from dual union select 42 from dual union select 43 from dual union select 44 from dual union select 45 from dual union select 46 from dual union select 47 from dual union select 48 from dual union select 49 from dual union select 50 from dual union select 51 from dual union select 52 from dual union select 53 from dual union select 54 from dual union select 55 from dual union select 56 from dual union select 57 from dual union select 58 from dual union select 59 from dual union select 60 from dual union select 61 from dual union select 62 from dual union select 63 from dual union select 64 from dual union select 65 from dual union select 66 from dual union select 67 from dual union select 68 from dual union select 69 from dual union select 70 from dual union select 71 from dual union select 72 from dual union select 73 from dual union select 74 from dual union select 75 from dual union select 76 from dual union select 77 from dual union select 78 from dual union select 79 from dual union select 80 from dual union select 81 from dual union select 82 from dual union select 83 from dual union select 84 from dual union select 85 from dual union select 86 from dual union select 87 from dual union select 88 from dual union select 89 from dual union select 90 from dual union select 91 from dual union select 92 from dual union select 93 from dual union select 94 from dual union select 95 from dual union select 96 from dual union select 97 from dual union select 98 from dual union select 99 from dual union select 100 from dual; 
 SELECT * FROM `DUAL` WHERE id>0 AND id<101 

上面的查询是用SQL在数据库中编写的。