日期:2014-05-17  浏览次数:20736 次

如何去掉数据表中对称的记录(SQL 高手请进)
描述:
数据myTable   ,其中包含字段testA,testB

表中的数据如下:
testA         testB
    a                 b
    b                 a
    c                 d
    d                 c

现在想编写一个SQL   语句过滤上面数据表,剩下下面的信息:
testA         testB
    a                 b
    c                 d

请问各位大侠SQL   语句怎么写?

------解决方案--------------------
你需要保留的a和b;或者是c和d之间是否存在大小关系.

不考虑需要保留的数据中的testA和testB之间满足大小关系
delete from myTable a where exists(
select 1 from myTable b where a.testA = b.testB and a.testB = b.testA and a.rowid > b.rowid ) ./*只判断是否存在对称重复的记录,删除rowid较大的记录*/

假设保留的均为testA <testB的记录的话,可以试下下面的语句.
delete from myTable a where exists(
select 1 from myTable b where a.testA = b.testB and a.testB = b.testA and a.rowid <> b.rowid ) and testA > testB.
/*判断存在对称重复的记录,对testA大于testB的记录进行删除*/