简体   繁体   English

MySQL中有类似的generate_series函数吗?

[英]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. 我想知道MySQL中是否有像generate_series这样的函数,该函数通常采用一个起始整数值,一个终止值和一个增量,并返回所有值的数组,包括开始和结束之间的和。 If there is no such function how would one go about declaring it in MySQL? 如果没有这样的函数,如何在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. MySQL不支持返回表的用户定义函数,因此您无法定义自己的函数。 In v8+, you can use a recursive CTE to generate numbers. 在v8 +中,您可以使用递归CTE生成数字。

However, the simplest method is probably to define your own numbers table. 但是,最简单的方法可能是定义自己的numbers表。 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. 当然, bigtable必须具有足够的行来容纳您要生成的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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