SQL - 插入并捕获 id 自动递增值 [英] SQL - INSERT and catch the id auto-increment value
问题描述
使用 SELECT 在同一个 SQL 中获取 auto-id 值的最佳方法是什么?
What is the best way to get the auto-id value in the same SQL with a SELECT?
一个论坛说添加这个; has Return Scope_Identity()
"
到底SQL在ASP中工作.
A forum said adding this "; has Return Scope_Identity()
"
in the end of the SQL works in ASP.
PHP中有没有对应的方式?
Is there a corresponding way in PHP?
推荐答案
这取决于您的数据库服务器.使用 MySQL,在插入查询后立即调用 mysql_insert_id()
.使用PostgreSQL,先查询"select nextval(seq)
" 并在插入查询中包含键.
It depends on your database server. Using MySQL, call mysql_insert_id()
immediately after your insert query. Using PostgreSQL, first query "select nextval(seq)
" on the sequence and include the key in your insert query.
如果另一个请求同时插入一条记录,则查询select max(id) + 1 from tbl
"可能会失败.
Querying for "select max(id) + 1 from tbl
" could fail if another request inserts a record simultaneously.
这篇关于SQL - 插入并捕获 id 自动递增值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!