MySQL 中的数字序列

发布于 2024-08-25 17:40:34 字数 76 浏览 5 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笔落惊风雨 2024-09-01 17:40:34

由于没有 xrange 这样的东西,因此可以使用一个单独的存储有整数的表(如前所述),或者只是创建一个存储过程来完成这项工作:

DROP PROCEDURE IF EXISTS xrange;
DELIMITER //
CREATE PROCEDURE xrange(x INT, y INT)
BEGIN
  DECLARE i INT DEFAULT x;
  CREATE TEMPORARY TABLE xrange_tmp (c1 INT);
  WHILE i < y DO
    INSERT INTO xrange_tmp VALUES (i);
    SET i = i + 1;
  END WHILE;
END;
//

用法:

CALL xrange(-2,10);
SELECT c1 FROM xrange_tmp;
DROP TABLE xrange_tmp;

上面的方法显然比创建一个准备好的表要慢与整数。不过它更灵活。

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:

DROP PROCEDURE IF EXISTS xrange;
DELIMITER //
CREATE PROCEDURE xrange(x INT, y INT)
BEGIN
  DECLARE i INT DEFAULT x;
  CREATE TEMPORARY TABLE xrange_tmp (c1 INT);
  WHILE i < y DO
    INSERT INTO xrange_tmp VALUES (i);
    SET i = i + 1;
  END WHILE;
END;
//

Usage:

CALL xrange(-2,10);
SELECT c1 FROM xrange_tmp;
DROP TABLE xrange_tmp;

The above is obviously going to be slower than creating a ready table with integers. It's more flexible though.

神仙妹妹 2024-09-01 17:40:34

您可以使用 LAST_INSERT_ID() 来模拟 MySQL 中的序列。

如果 expr 作为参数给出
LAST_INSERT_ID() 的值
参数由函数返回
并被记住为下一个值
由 LAST_INSERT_ID() 返回。这
可用于模拟序列:

创建一个表来保存序列
计数器并初始化它:

CREATE TABLE sequence (id INT NOT NULL);
INSERT INTO sequence VALUES (0);

使用该表生成如下所示的序列号:

  UPDATE sequence SET id=LAST_INSERT_ID(id+1);
  SELECT LAST_INSERT_ID();

UPDATE 语句增加
序列计数器并导致

<块引用>

下次调用LAST_INSERT_ID()
返回更新后的值。选择
语句检索该值。这
mysql_insert_id() C API函数可以
也可用于获取值。看
第21.9.3.37节,
“mysql_insert_id()”。

此处阅读更多内容以及有关这里

You can use LAST_INSERT_ID() to simulate sequences in MySQL.

If expr is given as an argument to
LAST_INSERT_ID(), the value of the
argument is returned by the function
and is remembered as the next value to
be returned by LAST_INSERT_ID(). This
can be used to simulate sequences:

Create a table to hold the sequence
counter and initialize it:

CREATE TABLE sequence (id INT NOT NULL);
INSERT INTO sequence VALUES (0);

Use the table to generate sequence numbers like this:

  UPDATE sequence SET id=LAST_INSERT_ID(id+1);
  SELECT LAST_INSERT_ID();

The UPDATE statement increments the
sequence counter and causes the

next call to LAST_INSERT_ID() to
return the updated value. The SELECT
statement retrieves that value. The
mysql_insert_id() C API function can
also be used to get the value. See
Section 21.9.3.37,
“mysql_insert_id()”.

Read up more here as well as some discussion on it here

爱*していゐ 2024-09-01 17:40:34

尝试使用整数表变体。此内容取自 Xaprb

create table integers(i int unsigned not null);
insert into integers(i) values (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);

select (hundreds.i * 100) + (tens.i * 10) + units.i as iii
from integers as units
    cross join integers as tens
    cross join integers as hundreds;

如果您最后选择一个名为xrange999的视图,然后您可以简单地:(

SELECT iii FROM xrange999 WHERE iii BETWEEN 0 AND 9

当然,您可以仅使用十行整数< /code> 表,但我发现一千个整数更有用。)

Try an integers table variant. This one is taken from Xaprb:

create table integers(i int unsigned not null);
insert into integers(i) values (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);

select (hundreds.i * 100) + (tens.i * 10) + units.i as iii
from integers as units
    cross join integers as tens
    cross join integers as hundreds;

If you made that last select a view named, say, xrange999, then you can simply:

SELECT iii FROM xrange999 WHERE iii BETWEEN 0 AND 9

(of course, you can do that with just the ten-row integers table, but I find a thousand integers a little more useful.)

情徒 2024-09-01 17:40:34

如果 Python 是您的编程语言,您可以映射相同的 Python 范围来创建以下形式的 INSERT 语句:

INSERT INTO Table (IDCol) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9)

不完全是您所要求的,但是一行 Python 代码可以处理最大长度为 MySQL 的任何范围陈述。

If Python is your programming language, you can map that same Python range to create an INSERT statement of the form:

INSERT INTO Table (IDCol) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文