MySQL 中的数字序列
在Python中,如果我想要一个从0到9(含)的序列,我会使用 xrange(0,10) 。有什么办法可以在 MySQL 中做到这一点吗?
In Python if I wanted a sequence from 0 - 9 (inclusive) I would use xrange(0,10) . Is there a way I can do this in MySQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于没有 xrange 这样的东西,因此可以使用一个单独的存储有整数的表(如前所述),或者只是创建一个存储过程来完成这项工作:
用法:
上面的方法显然比创建一个准备好的表要慢与整数。不过它更灵活。
Since there is no such thing as xrange, one could use a separate table stored with integer (as previously answered), or just make a stored procedure to do the job:
Usage:
The above is obviously going to be slower than creating a ready table with integers. It's more flexible though.
您可以使用 LAST_INSERT_ID() 来模拟 MySQL 中的序列。
在此处阅读更多内容以及有关这里
You can use LAST_INSERT_ID() to simulate sequences in MySQL.
Read up more here as well as some discussion on it here
尝试使用整数表变体。此内容取自 Xaprb:
如果您最后
选择
一个名为xrange999
的视图,然后您可以简单地:(当然,您可以仅使用十行
整数< /code> 表,但我发现一千个整数更有用。)
Try an integers table variant. This one is taken from Xaprb:
If you made that last
select
a view named, say,xrange999
, then you can simply:(of course, you can do that with just the ten-row
integers
table, but I find a thousand integers a little more useful.)如果 Python 是您的编程语言,您可以映射相同的 Python 范围来创建以下形式的 INSERT 语句:
不完全是您所要求的,但是一行 Python 代码可以处理最大长度为 MySQL 的任何范围陈述。
If Python is your programming language, you can map that same Python range to create an INSERT statement of the form:
Not exactly what you asked for, but a single line of Python code that will handle any range up to the maximum length of a MySQL statement.