gpt4 book ai didi

mysql - 从mysql表中删除重复行(稍微复杂)

转载 作者:行者123 更新时间:2023-11-29 06:48:13 25 4
gpt4 key购买 nike

我希望 mysql 中有一种简单的方法可以执行以下操作。

假设我有一个名为 T1 的表

+-------+-------+-------------+
| gene1 | gene2 | correlation |
+-------+-------+-------------+
| ABC | DEF | 0.1 |
+-------+-------+-------------+
| DEF | ABC | 0.1 |
+-------+-------+-------------+

我想删除 gene1gene2 转置且相关性相同的条目。

例如,IF (gene1 = 'ABC' AND Gene2 = 'DEF' AND correlation = 0.1) 那么我想删除这些条目。

最佳答案

如果您想要查询,只需执行以下操作:

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;

最终条件 (>) 是仅删除匹配的行之一。

关于mysql - 从mysql表中删除重复行(稍微复杂),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48574018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com