如何在MySQL中生成动态序列表?

发布于 2025-01-01 07:11:55 字数 1112 浏览 1 评论 0原文

我正在尝试在 MySQL 中生成一个序列表,以便我可以从 last_insert_id 获取唯一的 id。

问题是我动态地需要多个序列。


首先,我创建了一个表:

CREATE TABLE `sequence` (
  `label` char(30) CHARACTER SET latin1 NOT NULL,
  `id` mediumint(9) NOT NULL DEFAULT '0',
  PRIMARY KEY (`label`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

然后尝试使用 http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

UPDATE sequence SET id = LAST_INSERT_ID(id + 1) WHERE label = 'test';
SELECT LAST_INSERT_ID();

过了一会儿我意识到我还需要生成行安全地使用新标签。 所以我将这个模式更改为:

CREATE TABLE `sequence` (
  `label` char(30) CHARACTER SET latin1 NOT NULL,
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

我只是放弃使用 WHERE 子句来更新其 id。

INSERT INTO sequence (label) values ( ? )
SELECT LAST_INSERT_ID()

这是正确的方法吗?我想知道是否有更好的解决方案。

I'm trying to generate a sequence table in MySQL, so that I can get unique ids from last_insert_id.

The problem is that I need multiple sequences dynamically.


At the first, I created a table:

CREATE TABLE `sequence` (
  `label` char(30) CHARACTER SET latin1 NOT NULL,
  `id` mediumint(9) NOT NULL DEFAULT '0',
  PRIMARY KEY (`label`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

And then tried to get the number, using example from http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

UPDATE sequence SET id = LAST_INSERT_ID(id + 1) WHERE label = 'test';
SELECT LAST_INSERT_ID();

After a while I realized that I also need to generate rows for new labels safely.
So I changed this schema into:

CREATE TABLE `sequence` (
  `label` char(30) CHARACTER SET latin1 NOT NULL,
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

And I simply gave up using WHERE clause to update its id.

INSERT INTO sequence (label) values ( ? )
SELECT LAST_INSERT_ID()

Is this a proper way? I want to know if there is a better solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

说好的呢 2025-01-08 07:11:55

MyISAM 引擎将为您做这件事 -

表定义:

CREATE TABLE `sequence` (
  `label` char(30) CHARACTER SET latin1 NOT NULL,
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

填充表:

INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);
INSERT INTO sequence VALUES ('a', NULL);

INSERT INTO sequence VALUES ('b', NULL); -- add another labels 'b'
INSERT INTO sequence VALUES ('b', NULL);

INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);

显示结果:

SELECT * FROM sequence;
+-------+----+
| label | id |
+-------+----+
| a     |  1 |
| a     |  2 |
| a     |  3 |
| a     |  4 |
| a     |  5 |
| a     |  6 |
| b     |  1 |
| b     |  2 |
+-------+----+

The MyISAM engine will do it for you -

Table definition:

CREATE TABLE `sequence` (
  `label` char(30) CHARACTER SET latin1 NOT NULL,
  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`label`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Populate table:

INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);
INSERT INTO sequence VALUES ('a', NULL);

INSERT INTO sequence VALUES ('b', NULL); -- add another labels 'b'
INSERT INTO sequence VALUES ('b', NULL);

INSERT INTO sequence VALUES ('a', NULL); -- add some 'a' labels
INSERT INTO sequence VALUES ('a', NULL);

Show result:

SELECT * FROM sequence;
+-------+----+
| label | id |
+-------+----+
| a     |  1 |
| a     |  2 |
| a     |  3 |
| a     |  4 |
| a     |  5 |
| a     |  6 |
| b     |  1 |
| b     |  2 |
+-------+----+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文