防止重复输入的自动增量 [英] Prevent auto increment on duplicate entry

查看:175
本文介绍了防止重复输入的自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到这个问题(见底部的链接),但我似乎无法弄清楚一个答案。问题是我将数据插入到具有主键的自动增量ID的表上,另一个字段具有UNIQUE索引以避免重复。这样做,但是当这种情况发生时,ID增加,尽管没有数据被存储。

I have seen this issue around (See links at bottom) but I can't seem to figure out an answer. The problem is that I insert data on a table with an auto increment ID that is a primary key, and another field with a UNIQUE index to avoid duplicates. This works, but when that happens the ID is incremented, although no data has been stored.

最好删除自动增量并处理它我自己选择最大(ID)?

目前我已经尝试了几种策略包括 INSERT IGNORE INSERT ... ON DUPLICATE KEY UPDATE

At the moment I have tried several strategies to make it work as is, including INSERT IGNORE and INSERT ... ON DUPLICATE KEY UPDATE

我最近的尝试是使用以下查询:

My latest try was using the following query:

INSERT INTO
    content(field1, field2)
SELECT(:field1, :field2) FROM DUAL
WHERE NOT EXISTS(
    SELECT field1, field2
    FROM content
    WHERE field1 = :field1
)






相关


Related

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆