2012-01-08 45 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