測試此查詢
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
後, '時間' 爲 '編輯' 的委婉說法!?!?! – Strawberry 2013-02-11 10:18:13
您定義了一個唯一的密鑰,因爲您不需要重複的密鑰。現在你的UPDATE語句會生成重複密鑰,你正在尋找一種方法來忽略這個錯誤?這沒有任何意義,我... – CodeZombie 2013-02-11 10:34:47
@ZombieHunter是......這是發生,因爲一些數字存儲與,現在我想刪除像01268 20.3 ,我已經有一個不帶空格01268 20.3,有我面臨的問題 –
Harinder
2013-02-11 10:46:24