[英]Returning last inserted id from MySql
我在以下查询中遇到了麻烦:
START TRANSACTION;
SET @LASTID = 0;
INSERT INTO `Accounts` (`Col1`,`col2`,`col3`,`col4`)
VALUES (@param1,@param2,@param3,@param4);
SET @LASTID = last_insert_id(); -- This is what I need
INSERT INTO `Users` (`usr1`,`usr2`,`usr3`,`usr4`)
VALUES (@usr1,@usr2,@usr3,@usr4);
SELECT @LASTID;
COMMIT;
基本上,我需要从accounts表中返回最后插入的ID,但是在运行SELECT @LASTID时,MySql返回的是blob而不是单个值,这在C#asp.net中访问时遇到问题
有没有简单的方法来获取此值作为int / varchar? 我认为从代码中的Blob转换是过大的,我想把这一切留给Mysql服务器。
提前致谢。
采用
SELECT LAST_INSERT_ID();
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
将最后一条语句更改为:
SELECT CAST(@LASTID AS integer) as last_id_from_accounts;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.