欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

MySQL的LAST_INSERT_ID用法举例_MySQL

程序员文章站 2022-05-28 17:55:40
...
bitsCN.com MySQL的LAST_INSERT_ID用法举例 环境:MySQL Sever 5.1 + MySQL命令行工具首先看个例子(主键是自增长): [sql] mysql> insert into bankaccount(name,balance) values('123', 1000); Query OK, 1 row affected (0.06 sec) mysql> insert into bankstatement(action, txdate, amt, toaccno, fromaccno) values ('122', curdate(), 1000, 1, 2); Query OK, 1 row affected (0.00 sec) mysql> select last_insert_id(); +------------------+ | last_insert_id() | +------------------+ | 7 | +------------------+ 1 row in set (0.00 sec) mysql> select * from bankaccount; +-------+------+---------+ | accno | name | balance | +-------+------+---------+ | 1 | 张三 | 200 | | 2 | 李四 | 900 | | 3 | 123 | 1000 | | 4 | 123 | 1000 | +-------+------+---------+ 4 rows in set (0.00 sec) mysql> select * from bankstatement; +----+--------------+------------+------+---------+-----------+ | id | action | txdate | amt | toaccno | fromaccno | +----+--------------+------------+------+---------+-----------+ | 1 | 开户 | 2012-10-14 | 100 | NULL | 1 | | 2 | 开户 | 2012-10-14 | 1000 | NULL | 2 | | 3 | 查找账户信息 | 2012-10-14 | 0 | NULL | 2 | | 4 | 查找账户信息 | 2012-10-14 | 0 | NULL | 1 | | 5 | 转账 | 2012-10-14 | 100 | 1 | 2 | | 6 | 122 | 2012-10-14 | 1000 | 1 | 2 | | 7 | 122 | 2012-10-14 | 1000 | 1 | 2 | +----+--------------+------------+------+---------+-----------+ 7 rows in set (0.00 sec) 总结:LAST_INSERT_ID()返回最后一个INSERT或UPDATE语句中AUTO_INCREMENT列的值。
参考资料:

MySQL的LAST_INSERT_ID用法http:///database/201208/151747.html;mysql中的LAST_INSERT_ID()分析http:///database/201206/137028.htmlMysql函数:Last_insert_id()语法讲解http:///database/201210/160986.html bitsCN.com
相关标签: mysql