删除重复的记录,100跪求
我的比赛表A
ID name1 name2 比赛时间
1 A B 2007-09-04
2 B A 2007-09-04
3 A C 2007-09-03
4 C A 2007-09-03
5 B D 2007-09-02
6 D B 2007-09-02
我现在只想保留
2 B A 2007-09-04
4 C A 2007-09-03
6 D B 2007-09-02
因为A-> B和B-> A是同一场比赛,只要保留一条记录就行啦
请问这个sql怎么写呀?
------解决方案--------------------我的比赛表A
ID name1 name2 比赛时间
1 A B 2007-09-04
2 B A 2007-09-04
3 A C 2007-09-03
4 C A 2007-09-03
DELETE A
FROM 表A A
WHERE EXISTS(
SELECT * FROM 表A
WHERE 比赛日期 = A.比赛日期
AND id > A.id
AND name1 = A.name2
AND name2 = A.name2)
------解决方案--------------------delete t from A t where exists(select 1 from A where name1=t.name2 and name2=t.name1 and 比赛时间=t.比赛时间)
------解决方案--------------------delete
t
from
A t
where
exists(select 1 from A where name1=t.name2 and name2=t.name1 and 比赛时间=t.比赛时间 and id> t.id)
------解决方案--------------------DELETE A FROM 表A A
WHERE EXISTS( SELECT * FROM 表A
WHERE 比赛日期 = A.比赛日期
AND id > A.id AND
((name1 = A.name1 AND name2 = A.name2) or
(name2 = A.name1 AND name1 = A.name2)))
------解决方案--------------------或者
delete t1
from
A t1,A t2
where
t1.name1=t2.name2
and
t1.name2=t2.name1
and
t1.比赛时间=t2.比赛时间
and
t1.id <t2.id
------解决方案-----------------------保留ID大的那条
Delete T From A As T Where Exists
(Select 1 From A Where name1=T.name2 And name2=T.name1 And 比赛时间=T.比赛时间 And ID> T.ID)
---保留ID小的那条
Delete T From A As T Where Exists