在 INSERT INTO 语句期间访问自动递增值

发布于 2024-11-28 03:52:58 字数 255 浏览 5 评论 0原文

我目前正在使用MySQL。我有一个表,其中有一个 auto_increment 'id' 字段和一个 'imgname' 字段,其中包含作为图像文件名的字符串。

我需要使用 INSERT INTO 语句创建的 auto_increment 值生成“imgname”值。问题是,我不知道这个值,直到我可以使用 mysql_insert_id,在插入查询运行之后。我想知道是否可以在插入查询期间以某种方式访问​​该值,然后使用它在查询语句中生成我的字符串。

提前致谢。

I am currently using MySQL. I have a table that has an auto_increment 'id' field, and an 'imgname' field containing a string that is the file name of an image.

I need to generate the 'imgname' value using the auto_increment value that is create by an INSERT INTO statement. The problem is, I don't know this value until I can use mysql_insert_id, AFTER the insert query has run. I would like to know if it's possible to access this value DURING the insert query somehow and then use it to generate my string in the query statement.

Thanks in advance.

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

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

发布评论

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

评论(2

伤痕我心 2024-12-05 03:52:58

我会保持 idimgname 彼此独立,并在需要时在 SELECT 上将两者组合起来。如果需要足够频繁,创建视图

I would keep the id and imgname independent of each other and combine the two on SELECT when needed. If the need is frequent enough, create a view.

π浅易 2024-12-05 03:52:58

看看 LAST_INSERT_ID( ) 函数。如果性能不是问题,请定期INSERT,然后使用LAST_INSERT_ID()UPDATE,例如:

UPDATE table SET name = CONCAT(name, "-", LAST_INSERT_ID()) WHERE id = LAST_INSERT_ID();

Have a look at LAST_INSERT_ID() function. If performance is not an issue, INSERT regularly, and then UPDATE using LAST_INSERT_ID(), like:

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