2013-02-11 105 views
0

我有57K項與列comp_phone表(UNIQUE KEY Phonecomp_phone))現在我嘗試這個時間柱Mysql的更換並刪除舊條目

update uk_data set comp_phone= REPLACE(comp_phone,' ',' ') 

但因爲我有獨特的KEY本專欄我收到錯誤

[SQL] update uk_data set comp_phone= REPLACE(comp_phone,' ',' ') 

[Err] 1062 - Duplicate entry '01268 203***' for key 'Phone' 

所以我用

DELETE FROM uk_data WHERE comp_phone = '01268 203***' 

刪除舊行。但是這正在很長,因爲我不知道有多少行被複制後,更換所以是有一個簡單的方法來更換和刪除或覆蓋舊條目

THX

+1

後, '時間' 爲 '編輯' 的委婉說法!?!?! – Strawberry 2013-02-11 10:18:13

+0

您定義了一個唯一的密鑰,因爲您不需要重複的密鑰。現在你的UPDATE語句會生成重複密鑰,你正在尋找一種方法來忽略這個錯誤?這沒有任何意義,我... – CodeZombie 2013-02-11 10:34:47

+0

@ZombieHunter是......這是發生,因爲一些數字存儲與,現在我想刪除像01268 20.3 ,我已經有一個不帶空格01268 20.3,有我面臨的問題 – Harinder 2013-02-11 10:46:24

回答

1

測試此查詢

CREATE TEMPORARY table Kratika 
(
    _id int, 
    _value varchar(50) 
); 

INSERT INTO Kratika 
SELECT 1, '1111222255' UNION 
SELECT 2, '1111222 255' UNION -- duplicate 
SELECT 3, '11112222 55' UNION -- duplicate 
SELECT 4, '1111222233'; -- not duplicate 

SELECT * FROM Kratika; 

DELETE FROM Kratika WHERE _value <> REPLACE(_value, ' ', '') ; 

SELECT * FROM Kratika; 

輸出

1 1111222255 
2 1111222 255 
3 11112222 55 
4 1111222233 

-- Select after deletion of duplicate records 
    1 1111222255 
    4 1111222233 

編輯

如果您沒有問題while循環,那麼你可以測試這個(我不是一個MySQL開發人員,我不知道爲什麼MySQL的不是讓我寫while循環,所以創建PROC爲同)

DROP PROCEDURE IF EXISTS test_kratik; 
delimiter ## 

CREATE PROCEDURE test_kratik 
(

) 
BEGIN 
DROP table IF EXISTS Kratika; 
CREATE TEMPORARY table Kratika 
(
    _id int, 
    _value varchar(50) 
); 

INSERT INTO Kratika 
SELECT 1, '1111222255' UNION 
SELECT 2, '1111222 255' UNION -- duplicate 
SELECT 3, '11112222 55' UNION -- duplicate 
SELECT 4, '1111222233'; -- not duplicate 

SELECT * FROM Kratika; 

SET @ID := 0; 
SET @DUP_ID := 0; 
SET @value := ''; 

WHILE @ID <= (SELECT MAX(_id) FROM kratika) DO 
    BEGIN 
     IF EXISTS(SELECT _id from kratika WHERE _id = @ID) THEN 
      BEGIN 
       SET @value = (SELECT REPLACE(_value, ' ', '') from kratika WHERE _id = @ID); 
       DELETE FROM kratika 
       WHERE REPLACE(_value, ' ', '') = @value 
       AND _id <> @ID; 
      END; 
     END IF; 
     SET @ID = @ID + 1; 
    END; 
END WHILE; 

UPDATE kratika 
SET _value = REPLACE(_value, ' ', '') 
WHERE _value <> REPLACE(_value, ' ', '') ; 

SELECT * FROM kratika; 

END ## 

delimiter ; 

CALL test_kratik(); 

更新這將是很好,如果你不想while循環使用....

DROP TABLE IF EXISTS Kratika; 

CREATE TEMPORARY table Kratika 
(
    _id int, 
    _value varchar(50) 
); 

INSERT INTO Kratika 
SELECT 1, '1111222255' UNION 
SELECT 2, '1111222 255' UNION -- duplicate 
SELECT 3, '11112222 55' UNION -- duplicate 
SELECT 4, '111122 2233'; -- not duplicate 

DROP TABLE IF EXISTS copy_temp; 

CREATE TEMPORARY table copy_temp 
(
    _id1 int, 
    _value1 varchar(50) 
); 

INSERT INTO copy_temp 
SELECT _id, _value FROM kratika; 

SELECT * FROM Kratika; 

DELETE FROM Kratika 
WHERE _id <> (SELECT _id1 FROM copy_temp 
WHERE REPLACE(_value, ' ', '') = REPLACE(_value1, ' ', '') 
Limit 1 
); 

UPDATE kratika 
SET _value = REPLACE(_value, ' ', '') 
WHERE _value <> REPLACE(_value, ' ', '') ; 


SELECT * FROM Kratika; 

輸出

1 1111222255 
2 1111222 255 
3 11112222 55 
4 1111222233 

更新

1 1111222255 
4 1111222233 
+0

這將刪除如果重複與否其中獲得編輯沒有母校的所有行....( – Harinder 2013-02-11 11:53:29

+0

@ Harinder如果第4項是'4 111122 2233',那麼它將取代第4項刪除也是正確的。所以你想用'1111222233'權保留呢? – vikas 2013-02-11 12:01:14

+0

如果u放置11112222 33,空間就將它刪除,因爲查詢將刪除與空間的所有東西 – Harinder 2013-02-11 12:04:25