删除基于两列值的重复。

时间:2014-07-03 14:12:09

标签: mysql database excel openoffice-calc ods

我需要你们的帮助。

我的数据就像这样

A A value 1 value 2
A B value# value#
B A value# value#
D F value# value#
V F value# value#

我需要一行A B或B A.它应该看起来像这样

A A value 1 value 2
A B value# value#
B A value# value#  .....this record should be removed
D F value# value#
V F value# value#

请帮助解决这个问题。

谢谢大家

桑杰

1 个答案:

答案 0 :(得分:0)

使用您的示例案例,您可以执行类似

的操作
delete from table1 t1
where exists (select null
from table1
where t1.col1 = col2 and t1.col2 = col1 and t1.col1 > col1
)

但如果你有

,那就不会删除重复项
A B
A B => won't be removed
A A

如果您的表中有代理键(如自动增量ID),则可以执行此操作,这也会删除重复的A B

之一
delete from table1 t1
    where exists (select null
    from table1
    where ((t1.col1 = col2 and t1.col2 = col1) or (t1.col1 = col1 and t1.col2 = col2) and t1.id > id
    )
相关问题