我希望mysql中有一个简单的方法来执行以下操作。
假设我有一个名为 T1
的表格+-------+-------+-------------+
| gene1 | gene2 | correlation |
+-------+-------+-------------+
| ABC | DEF | 0.1 |
+-------+-------+-------------+
| DEF | ABC | 0.1 |
+-------+-------+-------------+
我想删除gene1
和gene2
转置且相关性相同的条目。
例如,IF (gene1 = 'ABC' AND gene2 = 'DEF' AND correlation = 0.1)
然后我想删除这些条目。
答案 0 :(得分:2)
如果您想要查询,只需执行以下操作:
select t.*
from t
where gene1 < gene2
union all
select t.*
from t
where gene1 > gene2 and
not exists (select 1
from t t2
where t.gene1 = t2.gene2 and t.gene2 = t2.gene1 and
t.correlation = t2.correlation
);
如果您确实要删除条目,可以执行以下操作:
delete t
from t join
t t2
on t.gene1 = t2.gene2 and t.gene2 = t.gene1 and
t2.correlation = t.correlation and
t.gene1 > t.gene2;
最终条件(>
)是您只删除其中一个匹配的行。