简体   繁体   中英

Is there an analogous generate_series function in MySQL?

I am wondering if there is a function in MySQL like generate_series which usually takes in a starting integer value, an ending one and an increment and returns a array of all the values including and between the start and the end. If there is no such function how would one go about declaring it in MySQL?

This is too long for a comment.

There is no such function. MySQL does not support user-defined functions that return tables, so you cannot define your own. In v8+, you can use a recursive CTE to generate numbers.

However, the simplest method is probably to define your own numbers table. Another alternative is to use any-old big table and do something like:

select (@rn := @rn + 1) as num
from bigtable t cross join
     (select @rn := 0) params
limit 100;  -- however many numbers you want

Of course, bigtable has to have enough rows for the numbers you want to generate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM