获取当前auto_increment值
我正在使用 php 为我的表执行 mysql 插入。 自动增量列名称为“link_id” “别名”列用于制作 SEO 友好的 url。
在插入过程中,我想在别名列的末尾附加 link_id 值。
所以我需要知道正在插入的 link_id 值是什么。
我不想再进行查询。
mysql_insert_id() 不起作用,因为它使用先前的查询,而不是当前的查询。
谢谢
I am doing mysql insert for my table using php.
Autoincrement column name is "link_id"
and "alias" colum is used to make SEO friendly url.
During my insert, I would like to attach link_id value at the end of my alias column.
So I need to know what is the being inserted link_id value.
I do not want to do another query.
mysql_insert_id() does not work, since its using previous query, and not current query.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用SHOW TABLE STATUS:
它将为您提供auto_increment列的当前状态。
编辑:
在一个查询中,您可以这样做:
它将在
column_name
列中为您提供新的auto_increment值。You should use
SHOW TABLE STATUS
:It will give you the current status of auto_increment column.
EDITED:
In one query you can do it like this:
It will give you the new auto_increment value in the column
column_name
.您是否考虑过 MySQL 中的 触发器?类似于:
编辑:我最近在一家公司工作,该公司的旗舰软件过去常常假设 max(id) + 1 = next id。问题是并发;这种情况很少见,但两个人可能会获得相同的 id,从而造成各种混乱。尝试预测该值时要非常小心。
Have you considered a trigger in MySQL? Something like:
EDIT: I was recently working for a company whose flagship software used to assume that max(id) + 1 = next id. The problem is concurrency; it's rare, but two people can get the same id, causing all sorts of chaos. Be extremely careful trying to predict the value.
我在 SELECT 端处理这种类型的场景。
例如:
插入表名(aliasroot)values('index.php?link_id=');
例如,
在我的选择中,我会执行“select concat(aliasroot,link_id) as alias from tablename”
I handle this type of scenario on the SELECT end.
For Example:
insert into tablename(aliasroot) values('index.php?link_id=');
This would give you for example
In my select I would do 'select concat(aliasroot,link_id) as alias from tablename'
您可以使用 mysql_insert_id() ++,尽管这并不总是可靠的。
更好的方法是插入查询,然后使用 CONCAT() 附加当前 id
you could use mysql_insert_id() ++, though that won't always be reliable.
what would be better would be to insert the query, then append the current id using CONCAT()