- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有这张 table
CREATE TABLE IF NOT EXISTS `t5` (
`id` int(11) NOT NULL auto_increment,
`a` int(11) NOT NULL,
`b` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a` (`a`,`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
a_b 是唯一键。
我有这样的 php 代码
$db = DBFactory::getInstance();
$db->selectDB('test');
$db->query("insert into t5 (a, b) values(1, 1) on duplicate key update a=1, b=1");
var_dump(mysql_insert_id());
$cur = $db->query("select last_insert_id()");
var_dump(mysql_fetch_assoc($cur));
在我的电脑上运行这段代码两次,结果是第一名
int(1)
array(1) {
["last_insert_id()"]=>
string(1) "1"
}
第二
int(1)
array(1) {
["last_insert_id()"]=>
string(1) "2"
}
你可以看到,mysql_insert_id() 两次都返回相同的值“1”,这对我来说很好,因为我想知道插入后的真实 id,而不是下一个 auto_increment 值。
但是当我在另一个环境中运行这段代码两次时:第一名
int(1)
array(1) {
["last_insert_id()"]=>
string(1) "1"
}
第二
int(2)
array(1) {
["last_insert_id()"]=>
string(1) "2"
}
正如您所看到的差异,第二次的结果 mysql_insert_id() 返回与 last_insert_id() 相同的值。
这个结果很糟糕,但我不知道为什么。我的代码在这两种环境中都运行良好大约 3 个月,直到今天才发生这种情况。有人可以解释吗?我确实在30天前将第二个环境的PHP版本升级到5.3.8,其他没有变化。这是错误吗?
更新
我切换到第三个mysql服务器(5.1.38-log),第二个插入返回 整数(0) 阵列(1){ ["last_insert_id()"]=> 字符串(1)“0”
所以我意识到问题可能与 mysql 版本有关。
最后,我把表定义改成了这个
DROP TABLE IF EXISTS `t5`;
CREATE TABLE IF NOT EXISTS `t5` (
`id` int(11) NOT NULL auto_increment,
`a` int(11) NOT NULL,
`b` int(11) NOT NULL,
`t` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a` (`a`,`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
同时编辑我的脚本
$db = DBFactory::getInstance();
$db->selectDB('test');
$db->query("insert into t5 (a, b, t) values(1, 1, ".time().") on duplicate key update a=1, b=1, t=".time());
//$db->query("insert ignore into t5 (a, b) values(1, 1)");
var_dump(mysql_insert_id());
$cur = $db->query("select last_insert_id()");
var_dump(mysql_fetch_assoc($cur));
不同的mysql服务器返回相同的mysql_insert_id但不同的last_insert_id()
5.0.24a-community-nt
int(1)
array(1) {
["last_insert_id()"]=>
string(1) "2"
}
5.0.51a-log
int(1)
array(1) {
["last_insert_id()"]=>
string(1) "2"
}
5.1.38-log
int(1)
array(1) {
["last_insert_id()"]=>
string(1) "0"
}
是否有任何系统变量控制这种行为?如果有人知道那将不胜感激。如果没有解决方案,我唯一能做的就是像这样强制插入以更新具有不同值的某些字段...
最佳答案
我认为您尝试使用 last_insert_id()
的方式并非本意 - 在这种情况下您没有插入任何内容,因此您也不应相信返回值。来自 MySQL docs :
If a table contains an AUTO_INCREMENT column and INSERT ... UPDATE inserts a row, the LAST_INSERT_ID() function returns the AUTO_INCREMENT value. If the statement updates a row instead, LAST_INSERT_ID() is not meaningful.
但是,似乎有一个解决方法(同一个文档)——手动设置 last_insert_id:
However, you can work around this by using LAST_INSERT_ID(expr). Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE
id=LAST_INSERT_ID(id), c=3;
关于php - mysql_insert_id 和 last_insert_id 错误行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8442926/
这个问题在这里已经有了答案: Easy mysql question regarding primary keys and an insert (4 个答案) 关闭 9 年前。 所以我想用公共(pu
我环顾四周,仍然对如何获取最后插入的 ID 感到困惑。我在正在执行的 mysql 语句末尾添加了语句 SELECT LAST_INSERT_ID();。我将值存储在 prID = Convert.To
所以我有如下 3 个表。 TOPICS TOPIC_TAGS Tags topic_id tag_id tag_id topic_data topic_id
$stmt2 = $db->prepare("INSERT INTO usertabbrige(`tabId`,`uId`)
我使用 PDO 在 PHP/MySql 上运行。 假设我有一个这样的查询。 $OrderId 在“Orders”表中自动递增。我可以使用 LAST_INSERT_ID() 在“Orders”和“Bou
CREATE FUNCTION `getSequenceNumber` ( company_id INTEGER, sequence_name varchar(255)) RETURNS INT(10
更新2:如果我手动将下面的EXECUTE代码示例替换为要执行的实际SQL代码,它工作得非常好,这告诉我这个问题已经厌倦了PREPARE、EXECUTE和DEALLOCATE PREPARE。 更新 1
我有以下代码。问题在于 SELECT LAST_INSERT_ID() 这总是返回最后插入的行。如果在获取最后一行之前实际插入了一行,我是否可以使用 INSERT IGNORE 检查,或者我是否可以简
我看不出我做错了什么,请帮忙。我正在学习 Select Last_Insert_Id,但我已经被困了几天了。 $result2 = $mysqli1->query("SELECT LAST_INSER
我有 3 个表: Words { wordId, name } WordGroups { wordGroupId, type} WordSets { wordSetId, wordId, wordGr
在 mysql 中创建线程安全序列时,我遇到了 mysql 文档 - https://dev.mysql.com/doc/refman/5.6/en/information-functions.htm
是否可以在插入查询中使用 LAST_INSERT_ID()? INSERT INTO mytable (col1, col2) VALUES ('val1', null), ('val2', LAST
我看过 official documentation但我还是有点困惑。 假设我有一个程序,它运行并执行插入,然后我请求 LAST_INSERT_ID(),我是从我的程序实例运行的插入中获取最后一个插入
我有一个包含三个查询的 mysql 事务 在第三个查询中,我试图从前两个查询中提取 last_insert_id 值。 VALUES ('".$result1[last_id]."','".$resu
我在 mysql 中有一个存储过程,它正在更新很多行然后插入一个(具体来说是分层数据结构)。除了我设置 @insert_id 变量的最后一段代码外,不要看太多代码。 CREATE DEFINER=`r
例如,如果我有插入查询: INSERT INTO user(username) VALUES('admin'); 然后用获取插入记录的id LAST_INSERT_ID(); 看起来找到了,但是如果在
我有两个表格,问题 和答案。answers 包含一个键 *question_id* 当我创建一个问题时,我将一条记录插入到questions 表中,并将几条记录插入到answers 表中。是否可以使用
我有这个问题:我有一个存储过程,它将行插入到具有自动递增列 a 的表 A,然后将一行插入到表 B,该表具有到列 A.a 的外键 b。我正在使用 LAST_INSERT_ID 获取列新插入行的值。但是
假设我们有 2 个数据库:a 和 b,以及表 a.test1 和 b.test2. 如果我需要在表a.test1中插入一行,并返回LAST_INSERT_ID()插入到b.test2中,将LAST_I
如果我有以下两个功能: int AccessDb::InsertColValue(string tableName, string col, string val) { try {
我是一名优秀的程序员,十分优秀!