AWS MySQL的LAST_INSERT_ID函数无法按预期工作。

huangapple go评论85阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年3月23日 04:40:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75817070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定