从MySQL返回最后插入的ID [英] Returning last inserted id from MySql

查看:231
本文介绍了从MySQL返回最后插入的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点麻烦用下面的查询:

I'm having a bit of trouble with the following query:

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;

基本上,我需要从账目表中返回最后插入的ID,但是运行SELECT @LASTID时,则MySQL返回一个blob,而不是一个单一的值,这我无法在C#asp.net访问

Basically, I need to return the last inserted ID from the accounts table, however when running SELECT @LASTID, MySql returns a blob rather than a single value, which I'm having trouble accessing in C# asp.net

有没有简单的方法来获得这个值作为一个int / VARCHAR?从斑点在code我觉得转换是矫枉过正了,我想,解除离开到MySQL服务器。

Is there any simple way to get this value as an int / varchar? Converting from blobs in code i feel is overkill, and I'd like to leave that lifting to the Mysql server.

先谢谢了。

推荐答案

使用

 SELECT LAST_INSERT_ID();

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

这篇关于从MySQL返回最后插入的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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