英文:
AWS MySQL LAST_INSERT_ID not working as expected
问题
使用RDS MySQL 8.0.28,具有以下表格:
CREATE TABLE temp(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
timestamp TIMESTAMP NOT NULL,
locale CHAR(30) NOT NULL,
content LONGTEXT NOT NULL
);
在插入了几个条目并使用mysqlsh
之后,以下查询成功返回:
SELECT content FROM temp WHERE id=(SELECT LAST_INSERT_ID());
+----------------------+
| content |
+----------------------+
| this is another test |
+----------------------+
1 row in set (0.0788 sec)
然而,在Lambda上运行时,它返回空。有任何想法为什么会这样?
这是唯一的替代方案吗?
SELECT content FROM temp ORDER BY id DESC LIMIT 1;
英文:
Using RDS MySQL 8.0.28, with the following table
CREATE TABLE temp(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
timestamp TIMESTAMP NOT NULL,
locale CHAR(30) NOT NULL,
content LONGTEXT NOT NULL
);
after inserting a couple of entries and using mysqlsh
, the following query returns successfully:
SELECT content FROM temp WHERE id=(SELECT LAST_INSERT_ID());
+----------------------+
| content |
+----------------------+
| this is another test |
+----------------------+
1 row in set (0.0788 sec)
however, ran on a lambda, it returns nothing. Any idea why?
Is this the only alternative?
SELECT content FROM temp ORDER BY id DESC LIMIT 1;
答案1
得分: 1
不要使用SELECT LAST_INSERT_ID(),这种方式行不通,而且它只适用于数据库连接的最后一个条目,而不是特定于客户端/表格的。参见LAST_INSERT_ID()相反,你可以使用以下语句:
SELECT content FROM temp WHERE id=(SELECT max(id) from temp);
英文:
Do not use SELECT LAST_INSERT_ID(), it wont work that way and also that is only for the last entry for the database connection , not client/table specific. see LAST_INSERT_ID() Instead , you could use
SELECT content FROM temp WHERE id=(SELECT max(id) from temp);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论