在 INSERT INTO 语句期间访问自动递增值
我目前正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会保持
id
和imgname
彼此独立,并在需要时在SELECT
上将两者组合起来。如果需要足够频繁,创建视图。I would keep the
id
andimgname
independent of each other and combine the two onSELECT
when needed. If the need is frequent enough, create a view.看看
LAST_INSERT_ID( )
函数。如果性能不是问题,请定期INSERT
,然后使用LAST_INSERT_ID()
UPDATE
,例如:Have a look at
LAST_INSERT_ID()
function. If performance is not an issue,INSERT
regularly, and thenUPDATE
usingLAST_INSERT_ID()
, like: