从MySQL返回最后插入的ID

编程入门 行业动态 更新时间:2024-10-27 00:31:02
本文介绍了从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访问

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

有没有简单的方法来获得这个值作为一个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();

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

更多推荐

从MySQL返回最后插入的ID

本文发布于:2023-10-13 20:45:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1489029.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MySQL   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!