2012-01-08 49 views
0

我有一個數據庫,有數千行中的25個的兩個副本。我怎樣才能執行一個命令來刪除其中的每一個?該表是包含2個外鍵的表。刪除SQL中的完整副本

例如:

cID | sID 
1 | 1 
1 | 23 
1 | 65 
2 | 45 
2 | 45 -> remove 
2 | 89 
3 | 1 
3 | 65 
3 | 107 
    ... 
+0

很不清楚,請用實例詳細說明 – 2012-01-08 17:20:29

+0

你只是想刪除每種類型的隨機行嗎? – kba 2012-01-08 17:21:05

+0

他們是完全一樣的,所以我不在乎哪個。 – 2012-01-08 17:23:37

回答

1

一種解決方案是創建另一個表:

create table replacement (cID ...., sID ....); 
# Only insert unique rows, which may be long 
insert into replacement select distinct cID, sID from origtable; 
# remove constraints from linked tables to origtable 
# add same constraints to replacement 
# add unique compound index on (cID,sID) to replacement 
drop table origtable; 
alter table replacement rename to origtable; 

這個假設當然,你origtable只包含這兩列。

0

SELECT DISTINCT是你的朋友。

+0

'表=從表中選擇不同*? – 2012-01-08 17:34:57

+0

閱讀爲僞代碼,是的。 – tobiasbayer 2012-01-08 17:39:34