2012-01-08 7 views
0

私は数千の行のうち25のコピーを2つ持つデータベースを持っています。これらのいずれかを削除するコマンドを実行するにはどうすればよいですか?テーブルは2つの外部キーを含むテーブルです。SQLで完全な重複を削除する

例:

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

非常に不明瞭で、例をあげてください。 –

+0

各タイプのランダムな行を削除したいだけですか? – kba

+0

彼らはまったく同じなので、私は気にしません。 –

答えて

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のみこれら2つの列を含んでいる、もちろん想定します。

0

SELECT DISTINCTはあなたの友達です。

+0

'table = select distinct * from table'? –

+0

疑似コードとして読み込みます。 – tobiasbayer

関連する問題