--建立测试环境
Create table TEST
(id Int IDENTITY(1,1),
col001 Varchar(10),
col002 Varchar(10)
)
--插入数据
Insert TEST Values('a','b')
Insert TEST Values('b','a')
Insert TEST Values('c','d')
Insert TEST Values('e','f')
Insert TEST Values('f','e')
--测试
Select * from TEST
Delete A from TEST A ,TEST B Where B.col001=A.col002 And B.col002=A.col001 And A.id>B.id
Select * from TEST
--Delete A from TEST A Where Exists (Select * from TEST Where col001=A.col002 And col002=A.col001 )--删除测试环境
Drop table TEST
--结果
/*
--删除前
id col001 col002
1 a b
2 b a
3 c d
4 e f
5 f e--删除后
id col001 col002
1 a b
3 c d
4 e f
*/
Insert InsertTEST Select * from TEST Where id Not In (Select A.id from TEST A ,TEST B Where B.col001=A.col002 And B.col002=A.col001 And A.id>B.id)如果InsertTEST不存在存在
Select * Into InsertTEST from TEST Where id Not In (Select A.id from TEST A ,TEST B Where B.col001=A.col002 And B.col002=A.col001 And A.id>B.id)
如果InsertTEST存在
Insert InsertTEST Select * from TEST Where id Not In (Select A.id from TEST A ,TEST B Where B.col001=A.col002 And B.col002=A.col001 And A.id>B.id)如果InsertTEST不存在
Select * Into InsertTEST from TEST Where id Not In (Select A.id from TEST A ,TEST B Where B.col001=A.col002 And B.col002=A.col001 And A.id>B.id)
where a.col001=b.col002 and a.col002=b.col001 and a.id>b.id
前一個是保留最小的一個記錄后一個是保留最大的一個記錄﹗﹗
不能刪的話就插入到一個虛擬表里之后再顯示就可以了
select a.* into #temp
where id not in
(select a.id from test a,test b
where a.col001=b.col002 and a.col002=b.col001 and a.id>b.id)select * from #temp