SQL SELECT to get the first N positive integers

前端 未结 10 1386

I need to get a result set containing the first N positive integers. Is it possible to use only standard SQL SELECT statement to get them (without any count table provided)?

10条回答
  •  半阙折子戏
    2020-11-30 13:10

    The sequence I propose allows the programmer to execute the following query :

    select value from sequence where value>=15 and value<100;
    

    And to obtain the expected results : the sequence of integers between 15 (inclusive) and 100 (exclusive).

    If that's what you want, you'll have to create the two following views, views that you'll declare only once :

    create view digits as select 0 n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9;
    
    create view sequence as select u.n+t.n*10+h.n*100 as value from digits as u cross join digits as t cross join digits as h;
    

    This way you have your sequence with an intuitive SELECT...

    Hope it helps.

提交回复
热议问题