怎么知道刚刚insert进去的记录的ID

csdn1111 2009-08-13 08:52:51
加精
create t1 (id bigint NOT NULL AUTO_INCREMENT, v varchar(100))


insert into t1 (v) values('sss')
怎么知道刚刚insert进去的记录的ID
...全文
6917 91 打赏 收藏 转发到动态 举报
写回复
用AI写文章
91 条回复
切换为时间正序
请发表友善的回复…
发表回复
cfx765226835 2011-08-29
  • 打赏
  • 举报
回复
看看、、、、
jklyekai 2009-10-21
  • 打赏
  • 举报
回复
完全 的sql门外汉,仰望。
csdn1111 2009-09-08
  • 打赏
  • 举报
回复
uuuppp
benettest 2009-08-26
  • 打赏
  • 举报
回复
语法难懂啊!!!
ytchangxiao 2009-08-24
  • 打赏
  • 举报
回复
对楼主这样的学习态度我不禁想破口大骂:"你个XXX!你不熟不会翻看帮助文档啊?"
文档里明确写明:
产生的ID 每次连接后保存在服务器中。这意味着函数向一个给定客户端返回的值是该客户端产生对影响AUTO_INCREMENT列的最新语句第一个 AUTO_INCREMENT值的。这个值不能被其它客户端影响,即使它们产生它们自己的 AUTO_INCREMENT值。这个行为保证了你能够找回自己的 ID 而不用担心其它客户端的活动,而且不需要加锁或处理。
人家这么基本的东西都想不到还怎么开发数据库?
另外:
select last_insert_id() + row_count() - 1 from T limit 1;
不加limit 1会返回多行,楼上的一部分出错就因这个问题.
文档里没有明确说明row_count()保存在连接中,不过,"人家这么基本的东西都想不到还怎么开发数据库?",
万一出错我不负责后果.
phpboy 2009-08-24
  • 打赏
  • 举报
回复
漂过
liuzuofei 2009-08-20
  • 打赏
  • 举报
回复
max(ID)为啥不行?
netxuning 2009-08-19
  • 打赏
  • 举报
回复
学习 。。。
asdf00010002 2009-08-19
  • 打赏
  • 举报
回复
output inserted.ID values (
jiangnanandi 2009-08-19
  • 打赏
  • 举报
回复
学习。。。。
tristan0429 2009-08-19
  • 打赏
  • 举报
回复
有并发根本不可能知道谁是最后插入的吧!!?

select @@identity 当表没有任何记录的时候不行吧


select case when max(id) is null then 0 else max(id) end as LastInsert from table;
mingfish2 2009-08-18
  • 打赏
  • 举报
回复
路过,学习了
red-fly 2009-08-18
  • 打赏
  • 举报
回复
记住先,我每次都不知道这种情况该如何处理
yuanshiren89 2009-08-18
  • 打赏
  • 举报
回复
学习了 我以前只知道用 max
love_study 2009-08-18
  • 打赏
  • 举报
回复
SELECT ID = scope_identity();
shangqt 2009-08-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 csdn1111 的回复:]
引用 1 楼 nianzhang747 的回复:
select last_insert_id();


这个根本不行,
你没有考虑到并发
[/Quote]

mysql 已经做了处理,select last_insert_id();只返回你最后插入的id,别人在期间插入的记录不影响它的返回值。
jackyoung02 2009-08-17
  • 打赏
  • 举报
回复
三种方法的比较
IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.

@@IDENTITY returns the last identity value generated for any table in the current session, across all scopes.

SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

The IDENT_CURRENT function returns NULL when the function is invoked on an empty table.


SQL Server 2005提供的新方法
CREATE TABLE Employees(
Id int IDENTITY(1,1) NOT NULL,   -- Other columns
, CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED (Id ASC) )

DECLARE @InsertedRows AS TABLE (Id int)
DECLARE @NewId AS INT INSERT INTO Employees
( /* column names */)
OUTPUT Inserted.Id INTO @InsertedRows
VALUES (/* column values */)
SELECT @NewId = Id FROM @InsertedRows

This code is not only concurrency-safe, but it allows us to get the values of other columns that could have been generated (e.g. by DEFAULTs or TRIGGERs)
lihan6415151528 2009-08-17
  • 打赏
  • 举报
回复
last_insert_id()
mad_13810376563 2009-08-17
  • 打赏
  • 举报
回复
学习
vip8086 2009-08-16
  • 打赏
  • 举报
回复
谁有好的方法wvyb分享下,,俺也正愁着呢
加载更多回复(70)

56,675

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧