MySQL 如何返回重复的唯一 ID?

发布于 2024-10-08 02:16:32 字数 164 浏览 1 评论 0原文

我在表中的几个字段上有一个唯一的键。使用 PHP,当我尝试插入相同的数据时,我会得到一个很好的错误。我试图归档的是 MySQL 给我返回生成错误的字段的主键。

那么,当我插入重复的内容时,MySQL 有没有什么方法可以返回“原始”ID,而不选择我一直尝试插入的所有字段?

提前Tnx

Ive got a unique key on a few fields in a table. With PHP I get a nice error like it should when I try to insert the same data. What Im trying to archieve is that MySQL gives me back the primary key of the field that generated the error.

So when I insert something duplicate, is there any way that MySQL can give back the 'origional' ID without selecting all the fields Ive been trying to insert?

Tnx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橪书 2024-10-15 02:16:32

您可以使用 mysql_error() 来获取类似 Duplicate entry '1' for key 'PRIMARY' 的消息,然后您可以解析该消息以找出重复的键值是什么。

You can use mysql_error() to get a message like Duplicate entry '1' for key 'PRIMARY' which you can then parse to figure out what the duplicate key value is.

半葬歌 2024-10-15 02:16:32

mysql 不能做到这一点..也许如果你为此创建程序或函数,这是可能的,但你应该想想你真的需要这个吗?

mysql can't do that.. maybe if you will make procedure or function for this it is posible but you shoud think are you realy need this?

十雾 2024-10-15 02:16:32

如果您有 AUTO_INCRMENT 字段(id 或其他字段),您可以使用 ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) 让 LAST_INSERT_ID 值返回插入“失败”时的 id 列。然而,它不会再失败,这是需要考虑的事情。

If you have an AUTO_INCREMENT field (id, or something else) you can use ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) to have the LAST_INSERT_ID value return the value of the id column when an insert "failed". It will however, not fail anymore, which is something to take into consideration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文