SQL SELECT to get the first N positive integers

前端 未结 10 1392

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:19

    Take a look at the following SO questions:

    • How to generate a range of numbers in Mysql
    • generate an integer sequence in MySQL

    Edit:

    Another aproach is to create a stored procedure that does that for you. PostgreSQL contains a function generate_series(start, stop) that does what you want.

    select * from generate_series(2,4);
     generate_series
    -----------------
                   2
                   3
                   4
    (3 rows)
    

    I'm not familar with MySQL but somthing like that should be easy to implement, if you are okay with SPs. This site shows an implemetation.

提交回复
热议问题