博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How Can I Get the Unique ID for the Last Inserted Row
阅读量:7040 次
发布时间:2019-06-28

本文共 1309 字,大约阅读时间需要 4 分钟。

24.1.4.3 How Can I Get the Unique ID for the Last Inserted

Row?

/*********** the following is from manual of MySQL ************/

If you insert a record in a table containing a column that has the AUTO_INCREMENT attribute,
you can get the most recently generated ID by calling the mysql_insert_id() function.
You can also retrieve the ID by using the LAST_INSERT_ID() function in a query string
that you pass to mysql_query().
You can check if an AUTO_INCREMENT index is used by executing the following code. This
also checks if the query was an INSERT with an AUTO_INCREMENT index:

if (mysql_error(&mysql)[0] == 0 && mysql_num_fields(result) == 0 && mysql_insert_id(&mysql) != 0) {
used_id = mysql_insert_id(&mysql); }

The most recently generated ID is maintained in the server on a per-connection basis. It
will not be changed by another client. It will not even be changed if you update another
AUTO_INCREMENT column with a non-magic value (that is, a value that is not NULL and not
0).
If you want to use the ID that was generated for one table and insert it into a second table,
you can use SQL statements like this:

INSERT INTO foo (auto,text) VALUES(NULL,’text’);            # generate ID by inserting NULL INSERT INTO foo2 (id,text) VALUES(LAST_INSERT_ID(),’text’);      # use ID in second table

转载地址:http://ytxal.baihongyu.com/

你可能感兴趣的文章
jQuery-easyui和validate表单验证实例
查看>>
【对讲机的那点事】5G时代的到来,2G和3G将会被关停?
查看>>
DeepLearning.ai学习笔记(二)改善深层神经网络:超参数调试、正则化以及优化--week3 超参数调试、Batch正则化和程序框架...
查看>>
NSMutableArray 简单细说
查看>>
Docker 助力百年研发院校的现代化改造,让 IT 资源物尽其用!
查看>>
各个 C# 版本的主要特性、发布日期和发布方式(C# 1.0 - 7.3)
查看>>
什么是nofollow标签?如何影响网站排名?
查看>>
开源技术“打进”好莱坞,学术软件基金会成立
查看>>
Android项目实战(十四):TextView显示html样式的文字
查看>>
你真的了解Python的字符串吗?
查看>>
创建ORC结果表
查看>>
从普通程序员到身价过百亿:追求长期价值的耐心,决定了你能走多远
查看>>
比原链Bytom错误码一览
查看>>
Windows CMD中的findstr命令详解
查看>>
破天荒!苹果终于对 MacBook Air 大更新,还有全新 iPad Pro 和 Mac mini
查看>>
肿瘤研发机构NMS启动新一轮融资,并将赴港上市
查看>>
【分享】学Java的必须练手的200个Java虚拟机相关的例子
查看>>
Web SSM 入坑
查看>>
KM算法入门
查看>>
使用 pymysql 操作MySQL数据库
查看>>