gpt4 book ai didi

mysql - 从数据库表中删除冗余行

转载 作者:行者123 更新时间:2023-11-30 22:33:18 24 4
gpt4 key购买 nike

我有一个如下所示的数据库表(我知道设计不好,但有很多这样的行):

person1    |    person2     |    counselor
Jane Doe | John Doe | Mary Smith
John Doe | Jane Doe | Mary Smith
Frank Jones| Ann Jones | Tom Jones
Ann Jones | Frank Jones | Tom Jones

我想弄清楚如何只选择“唯一”行之一,以便结果看起来像:

person1    |    person2     |    counselor
Jane Doe | John Doe | Mary Smith
Frank Jones| Ann Jones | Tom Jones

我已经尝试过各种方法,例如 SELECT distinctSELECT MIN(person1) 等,但我删除了。

最佳答案

您将有 (person1,person2,counselor) 的 6 个排列,您可以对所有排列使用 union。最后使用 where 子句,这样每个组合只会返回一行。

Fiddle with sample data

select * from (
select person1,person2,counselor
from tablename
union
select person1,counselor,person2
from tablename
union
select person2,person1,counselor
from tablename
union
select person2,counselor,person1
from tablename
union
select counselor,person2,person1
from tablename
union
select counselor,person1,person2
from tablename) t
where person1 < person2 and person2 < counselor

关于mysql - 从数据库表中删除冗余行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262533/

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