我試圖更新包含兩列的表; id
和position
。 id
是唯一的,並自動遞增。 position
是唯一的,但不會自動增加,實際上必須手動設置。在一個查詢中增加每行中的唯一列
我需要更改所有行的位置大於或等於5
(或我提供的任何其他數字)以將它們全部遞增。這是我的代碼:
UPDATE slides
SET position = position + 1
WHERE position >= 5;
不幸的是,它返回以下錯誤:
Error : Duplicate entry '2' for key 'slides_position_unique'
如何更新所有這些獨特的數量,而不會造成衝突,我這樣做?我已經嘗試了一個查詢所有可更新行的子查詢,並將其返回,但它無濟於事。
CREATE TABLE slides
(id int(10) unsigned NOT NULL AUTO_INCREMENT,
user_id int(10) unsigned NOT NULL,
position int(10) unsigned NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY slides_position_unique (position),
KEY slides_user_id_foreign (user_id),
CONSTRAINT slides_user_id_foreign
FOREIGN KEY (user_id) REFERENCES users (id))
'show create table'slides'的輸出是什麼? – Asenar
不要將其他信息添加到評論中,而是編輯您的問題並將新信息添加到問題中。這次我爲你做了。請記住這一點在未來。謝謝。 –