php中mysql每月自动从1开始递增

发布于 2024-12-12 04:02:20 字数 82 浏览 0 评论 0原文

我有一个解决方案,我需要一个表中的 id 从 1 开始每个月。我正在考虑使用月份名称作为主键以及 id 自动递增。我是不是方法错了?有人有什么想法吗?

I have solution where i need that id in one table starts every month from 1. I'm thinking of using month name as primary key together with id auto incerement. Am I wrong way? Does someone have any idea?

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

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

发布评论

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

评论(3

£冰雨忧蓝° 2024-12-19 04:02:20

MYISAM 表的示例:

CREATE TABLE table1(
  yearmonth_id INT(11) NOT NULL, -- combination of year and month
  id INT(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (yearmonth_id, id)
)
ENGINE = MYISAM;

INSERT INTO table1 VALUES(201110, NULL);
INSERT INTO table1 VALUES(201110, NULL);
INSERT INTO table1 VALUES(201110, NULL);

INSERT INTO table1 VALUES(201112, NULL);
INSERT INTO table1 VALUES(201112, NULL);
INSERT INTO table1 VALUES(201112, NULL);

INSERT INTO table1 VALUES(201201, NULL);
INSERT INTO table1 VALUES(201201, NULL);
INSERT INTO table1 VALUES(201201, NULL);

SELECT * FROM table1;
+--------------+----+
| yearmonth_id | id |
+--------------+----+
|       201110 |  1 |
|       201110 |  2 |
|       201110 |  3 |
|       201112 |  1 |
|       201112 |  2 |
|       201112 |  3 |
|       201201 |  1 |
|       201201 |  2 |
|       201201 |  3 |
+--------------+----+

Example with MYISAM table:

CREATE TABLE table1(
  yearmonth_id INT(11) NOT NULL, -- combination of year and month
  id INT(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (yearmonth_id, id)
)
ENGINE = MYISAM;

INSERT INTO table1 VALUES(201110, NULL);
INSERT INTO table1 VALUES(201110, NULL);
INSERT INTO table1 VALUES(201110, NULL);

INSERT INTO table1 VALUES(201112, NULL);
INSERT INTO table1 VALUES(201112, NULL);
INSERT INTO table1 VALUES(201112, NULL);

INSERT INTO table1 VALUES(201201, NULL);
INSERT INTO table1 VALUES(201201, NULL);
INSERT INTO table1 VALUES(201201, NULL);

SELECT * FROM table1;
+--------------+----+
| yearmonth_id | id |
+--------------+----+
|       201110 |  1 |
|       201110 |  2 |
|       201110 |  3 |
|       201112 |  1 |
|       201112 |  2 |
|       201112 |  3 |
|       201201 |  1 |
|       201201 |  2 |
|       201201 |  3 |
+--------------+----+
一江春梦 2024-12-19 04:02:20

我是不是走错了路?

确切地。

NB 的评论应该是一个答案。在这里引用一下:

为什么每个人都想篡改 auto_increment,而不是创建自己的触发器并以自己的方式实现逻辑......只是不要篡改主键,从多个角度来看,这是一个坏主意。创建一个触发器来增加其他列中的数字,并让主键执行它的操作。

每个网络程序员新手都应该把它刻在桌子上,并每天大声朗读 3 遍。

Am I wrong way?

Exactly.

A comment from the N.B. should be an answer. Quoting it here:

Why do everyone want to tamper with auto_increment instead of creating their own triggers and implementing the logic their way.. just don't tamper with the primary key, it's a bad idea from multiple perspectives. Create a trigger to increment number in some other column and let primary key do what it does.

Every newbie web-programmer should engrave it on their table and read aloud for the 3 times a day.

老街孤人 2024-12-19 04:02:20

主键的特点是它们通常需要简单。大多数系统使用数字的原因是因为在代码中,您只需传递数字来表示数据片段,而不必请求所有数据(如果不需要)。

您可以拥有一个多列主键,其中一列是月年字符串,另一列是数字自动增量,那么自动增量将在月份更改时自动重用数字。这样做的优点是可以直接绑定到表定义中,从而使系统迁移变得更加容易。

来源:输入此处链接说明

The thing with primary keys is that they need to be simple, normally. the reason most systems use numbers is because then in code, you just pass the number around to represent the piece of data, with out necessarily requesting all the data, if you dont need it.

You can have a multi-column primary key with one column being a month-year string and another being a numerical auto-increment, then the auto-increment will automatically re-use numbers when the month changes. This has the advantage of being tied directly into the table definition, which makes moving the system easier.

Source: enter link description here

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