SQL-插入并捕获id自动增加值 [英] SQL - INSERT and catch the id auto-increment value

查看:166
本文介绍了SQL-插入并捕获id自动增加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SELECT在同一SQL中获取自动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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆